Commit a5e1f2fb

a5e1f2fb31d4Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(stringio): truncate(N > len) grows + zero-fills (#816)

CRuby's StringIO#truncate(N) where N > current length pads the buffer with NULs up to N. spinel previously short-circuited when length >= sio->len, leaving the buffer unchanged so subsequent .string.length returned the old shorter length.

When length > sio->len: grow the buffer through sio_grow, memset the new tail to zero, advance sio->len.

Closes #816

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
a5e1f2fb31d4
Parent
c24ef98fd187

lib/stringio.cmodifiedView file

@@ -234,10 +234,20 @@
234234
235235 /* truncate(length) — truncate buffer to given length */
236236 int64_t sp_StringIO_truncate(sp_StringIO *sio, int64_t length) {
237+ if (!sio) return 0;
237238 if (length < 0) length = 0;
238239 if (length < sio->len) {
239240 sio->len = length;
240241 sio->buf[length] = '\0';
242+ } else if (length > sio->len) {
243+ /* Issue #816: grow the buffer + NUL-fill the gap, matching
244+ CRuby's "truncate(N) where N > current length pads with NULs". */
245+ sio_grow(sio, length - sio->pos);
246+ if (length > sio->len) {
247+ memset(sio->buf + sio->len, 0, (size_t)(length - sio->len));
248+ }
249+ sio->len = length;
250+ if (length < sio->cap) sio->buf[length] = '\0';
241251 }
242252 return 0;
243253 }