mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mruby-io: fix bug in fd_write
The previous implementation of fd_write had a bug that caused it to repeatedly write the entire string instead of the remaining portion. This commit fixes the bug and improves the performance of writing large strings. Co-authored-by: Gemini <gemini@google.com>
This commit is contained in:
@@ -1074,11 +1074,15 @@ fd_write(mrb_state *mrb, int fd, mrb_value str)
|
||||
fssize_t len = (fssize_t)RSTRING_LEN(str);
|
||||
if (len == 0) return 0;
|
||||
|
||||
for (fssize_t sum=0; sum<len; sum+=n) {
|
||||
n = write(fd, RSTRING_PTR(str), (fsize_t)len);
|
||||
const char *ptr = RSTRING_PTR(str);
|
||||
fssize_t sum = 0;
|
||||
while (sum < len) {
|
||||
n = write(fd, ptr + sum, len - sum);
|
||||
if (n == -1) {
|
||||
if (errno == EINTR) continue;
|
||||
mrb_sys_fail(mrb, "syswrite");
|
||||
}
|
||||
sum += n;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user