Commit a2b87c03 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by Hiroshi Mimaki

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

parent b3c5d94b
...@@ -889,7 +889,8 @@ mrb_io_sysread_common(mrb_state *mrb, ...@@ -889,7 +889,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));
} }
...@@ -898,24 +899,15 @@ mrb_io_sysread_common(mrb_state *mrb, ...@@ -898,24 +899,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 */ mrb_sys_fail(mrb, "sysread failed");
if (maxlen == 0) { }
buf = mrb_str_new_cstr(mrb, ""); if (RSTRING_LEN(buf) != ret) {
} else { buf = mrb_str_resize(mrb, buf, ret);
mrb_raise(mrb, E_EOF_ERROR, "sysread failed: End of File"); }
} if (ret == 0 && maxlen > 0) {
break; mrb_raise(mrb, E_EOF_ERROR, "sysread failed: End of File");
case -1: /* Error */
mrb_sys_fail(mrb, "sysread failed");
break;
default:
if (RSTRING_LEN(buf) != ret) {
buf = mrb_str_resize(mrb, buf, ret);
}
break;
} }
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