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:
Yukihiro "Matz" Matsumoto
2025-08-12 08:59:33 +09:00
parent 3246dd2562
commit f8ee815468
+6 -2
View File
@@ -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;
}