Commit 6fc7c19c

6fc7c19cd0aeMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

fix(runtime,codegen): preserve embedded NUL bytes through string length / index ops

Spinel-allocated strings track byte length in the sp_str_hdr header (written by sp_str_alloc and read by sp_str_byte_len), but several runtime / codegen surfaces walked the buffer via NUL-terminated while(*p) / strlen() semantics instead. A 0.chr value with header.len == 1 and byte content \x00 reported length == 0 and bytesize == 0; indexing past an embedded NUL in "x" + 0.chr + "y" returned the wrong char because sp_utf8_byte_offset stopped at the first NUL.

Three NUL-bound surfaces fixed:

  • sp_str_length: walks [s, s + sp_str_byte_len(s)) instead of while(*s). Codepoint count now reflects the header's view of the buffer.
  • sp_utf8_byte_offset: same [s, s+bl) bound so a char_idx past an embedded NUL still advances by the requested codepoint count. Fixes s[i] / s[i, n] for i past a NUL.
  • codegen bytesize: emits (mrb_int)sp_str_byte_len(rc) instead of (mrb_int)strlen(rc). Heap-allocated strings now report the header length; raw C string literals still fall through to strlen via sp_str_byte_len's marker check.

Sam Ruby's full hex-walk repro (#593) now matches CRuby byte-for-byte:

a=0.chr len=1 bytes=00 b=0.chr+0xc8.chr len=2 bytes=00c8 c=x+0.chr+y len=3 bytes=780079 d=progressive len=4 bytes=817e00c8 e=d[0,4] len=4 bytes=817e00c8

Scope: this commit fixes the read-side NUL-awareness. Other ops that still call strlen/strstr internally (str#split with NUL in the source string, str#index with a NUL needle) remain NUL-bound because their implementations route through C library routines. Those edge cases didn't surface in the WebSocket frame-building shape that motivated #593 (Ori's workaround uses byte accumulation via the C side; the in-memory representation is now correct for the read path his hex-walker exercises).

make test: 575/575 pass. make optcarrot: checksum 59662 stable. Stage-2 bootstrap fixpoint clean.

Closes #593

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

Details

Commit
6fc7c19cd0ae
Parent
76e6991b0289

lib/sp_runtime.hmodifiedView file

@@ -276,7 +276,7 @@
276276 static inline int sp_utf8_advance(const char*p){int cn=sp_utf8_char_len((unsigned char)*p);int i=1;while(i<cn&&((unsigned char)p[i]&0xC0)==0x80)i++;return i;}
277277 static inline int sp_utf8_decode(const char*p,uint32_t*out){unsigned char c=(unsigned char)p[0];if(c<0x80){*out=c;return 1;}if(c<0xC0){*out=c;return 1;}unsigned char c1=(unsigned char)p[1];if((c1&0xC0)!=0x80){*out=c;return 1;}if(c<0xE0){*out=((uint32_t)(c&0x1F)<<6)|(c1&0x3F);return 2;}unsigned char c2=(unsigned char)p[2];if((c2&0xC0)!=0x80){*out=c;return 1;}if(c<0xF0){*out=((uint32_t)(c&0x0F)<<12)|((uint32_t)(c1&0x3F)<<6)|(c2&0x3F);return 3;}unsigned char c3=(unsigned char)p[3];if((c3&0xC0)!=0x80){*out=c;return 1;}*out=((uint32_t)(c&0x07)<<18)|((uint32_t)(c1&0x3F)<<12)|((uint32_t)(c2&0x3F)<<6)|(c3&0x3F);return 4;}
278278 static inline int sp_utf8_encode(uint32_t cp,char*out){if(cp<0x80){out[0]=(char)cp;return 1;}if(cp<0x800){out[0]=(char)(0xC0|(cp>>6));out[1]=(char)(0x80|(cp&0x3F));return 2;}if(cp<0x10000){out[0]=(char)(0xE0|(cp>>12));out[1]=(char)(0x80|((cp>>6)&0x3F));out[2]=(char)(0x80|(cp&0x3F));return 3;}out[0]=(char)(0xF0|(cp>>18));out[1]=(char)(0x80|((cp>>12)&0x3F));out[2]=(char)(0x80|((cp>>6)&0x3F));out[3]=(char)(0x80|(cp&0x3F));return 4;}
279-static mrb_int sp_str_length(const char*s){mrb_int n=0;while(*s){s+=sp_utf8_advance(s);n++;}return n;}
279+static mrb_int sp_str_length(const char*s){size_t bl=sp_str_byte_len(s);const char*end=s+bl;mrb_int n=0;while(s<end){s+=sp_utf8_advance(s);n++;}return n;}
280280 static mrb_int sp_str_ord(const char*s){if(!*s)return 0;uint32_t cp;sp_utf8_decode(s,&cp);return(mrb_int)cp;}
281281 /* NULL-safe string equality. ENV[] returns NULL for unset vars
282282 (the dispatch is `sp_str_dup_external(getenv(...))`, which propagates
@@ -285,7 +285,7 @@
285285 Ruby; nil == nil is true, so falling back to pointer equality on the
286286 NULL path covers both. */
287287 static inline int sp_str_eq(const char*a,const char*b){if(!a||!b)return a==b;return strcmp(a,b)==0;}
288-static size_t sp_utf8_byte_offset(const char*s,mrb_int char_idx){const char*p=s;while(char_idx>0&&*p){p+=sp_utf8_advance(p);char_idx--;}return(size_t)(p-s);}
288+static size_t sp_utf8_byte_offset(const char*s,mrb_int char_idx){size_t bl=sp_str_byte_len(s);const char*p=s;const char*end=s+bl;while(char_idx>0&&p<end){p+=sp_utf8_advance(p);char_idx--;}return(size_t)(p-s);}
289289 static uint32_t*sp_utf8_decode_all(const char*s,size_t*out_n){size_t cap=8,n=0;uint32_t*cps=(uint32_t*)malloc(cap*sizeof(uint32_t));const char*p=s;while(*p){if(n>=cap){cap*=2;cps=(uint32_t*)realloc(cps,cap*sizeof(uint32_t));}uint32_t cp;p+=sp_utf8_decode(p,&cp);cps[n++]=cp;}*out_n=n;return cps;}
290290 static int sp_utf8_set_has(const uint32_t*cps,size_t n,uint32_t cp){for(size_t i=0;i<n;i++)if(cps[i]==cp)return 1;return 0;}
291291