Fix `IO#sysread` to update buffer string on `EOF`; ref #4982

parent 46857811
...@@ -927,7 +927,8 @@ mrb_io_sysread_common(mrb_state *mrb, ...@@ -927,7 +927,8 @@ mrb_io_sysread_common(mrb_state *mrb,
if (RSTRING_LEN(buf) != maxlen) { if (RSTRING_LEN(buf) != maxlen) {
buf = mrb_str_resize(mrb, buf, maxlen); buf = mrb_str_resize(mrb, buf, maxlen);
} else { }
else {
mrb_str_modify(mrb, RSTRING(buf)); mrb_str_modify(mrb, RSTRING(buf));
} }
...@@ -936,24 +937,15 @@ mrb_io_sysread_common(mrb_state *mrb, ...@@ -936,24 +937,15 @@ mrb_io_sysread_common(mrb_state *mrb,
mrb_raise(mrb, E_IO_ERROR, "not opened for reading"); mrb_raise(mrb, E_IO_ERROR, "not opened for reading");
} }
ret = readfunc(fptr->fd, RSTRING_PTR(buf), (fsize_t)maxlen, offset); ret = readfunc(fptr->fd, RSTRING_PTR(buf), (fsize_t)maxlen, offset);
switch (ret) { if (ret < 0) {
case 0: /* EOF */
if (maxlen == 0) {
buf = mrb_str_new_cstr(mrb, "");
} else {
mrb_raise(mrb, E_EOF_ERROR, "sysread failed: End of File");
}
break;
case -1: /* Error */
mrb_sys_fail(mrb, "sysread failed"); mrb_sys_fail(mrb, "sysread failed");
break; }
default:
if (RSTRING_LEN(buf) != ret) { if (RSTRING_LEN(buf) != ret) {
buf = mrb_str_resize(mrb, buf, ret); buf = mrb_str_resize(mrb, buf, ret);
} }
break; if (ret == 0 && maxlen > 0) {
mrb_raise(mrb, E_EOF_ERROR, "sysread failed: End of File");
} }
return buf; return buf;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment