From 04b20e9d4d490c5e3abe89cc1801f13a27a3eeb7 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Sat, 7 Feb 2026 19:50:46 -0500 Subject: [PATCH] Set string length in string_spec_RSTRING_PTR_read Since we do not set the length between the call to `read(fd, buffer, 30)` and `rb_str_modify_expand(str, 53)`, the length of the string is technically zero. If the call to `rb_str_modify_expand` actually expands the buffer, it will overwrite all the contents from the `read`. --- optional/capi/ext/string_spec.c | 1 + 1 file changed, 1 insertion(+) diff --git a/optional/capi/ext/string_spec.c b/optional/capi/ext/string_spec.c index 74aa9e56e..5a3c894dc 100644 --- a/optional/capi/ext/string_spec.c +++ b/optional/capi/ext/string_spec.c @@ -393,6 +393,7 @@ VALUE string_spec_RSTRING_PTR_read(VALUE self, VALUE str, VALUE path) { if (read(fd, buffer, 30) < 0) { rb_syserr_fail(errno, "read"); } + rb_str_set_len(str, 30); rb_str_modify_expand(str, 53); rb_ary_push(capacities, SIZET2NUM(rb_str_capacity(str)));