Commit 80783074

807830746553Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Fix sp_int_codepoint_to_str to set the string length

The function wrote the UTF-8 encoding + NUL but never set the string's byte length, leaving it at the 5-byte allocation. Legacy didn't hit this (it appends via sp_String_append, which is strlen-based), but the C backend's s << int uses sp_str_concat (byte_len-based), which then copied the trailing NULs. Set the length to the encoded length.

Fixes string_lshift_int_codepoint. make test: 33.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
807830746553
Parent
60011cb0b479

lib/sp_runtime.hmodifiedView file

@@ -482,9 +482,10 @@
482482 and return a NUL-terminated string. */
483483 static const char *sp_int_codepoint_to_str(mrb_int n) {
484484 char *s = sp_str_alloc_raw(5);
485- if (n < 0 || n > 0x10FFFF) { s[0] = 0; return s; }
485+ if (n < 0 || n > 0x10FFFF) { s[0] = 0; sp_str_set_len(s, 0); return s; }
486486 int len = sp_utf8_encode((uint32_t)n, s);
487487 s[len] = 0;
488+ sp_str_set_len(s, (size_t)len); /* byte_len must be the encoded length, not the alloc */
488489 return s;
489490 }
490491 /* Direct-mapped pointer-keyed cache for (byte_len, char_len). Populated lazily