Commit d77a44b9

d77a44b94cd2Merged

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

Description

fix(runtime): String NUL-bound slice + bytes[i] survive embedded zeros (#657)

#657 followup to #626. Concat already round-trips NULs (sp_str_concat walks via sp_str_byte_len for both inputs), but two surviving paths truncated past the first NUL byte:

  • sp_str_bytes walked for (i=0; s[i]; i++) and stopped at the first NUL. Now bounds on sp_str_byte_len(s), which honours the 0xfe/0xfc heap-string hdr marker.

  • sp_str_sub_range / sp_str_sub_range_len computed bend via a second sp_utf8_byte_offset call on the mid-string pointer (s + boff). That inner call fell back to strlen (no marker visible at the mid-string [-1] byte) and stopped at the NUL. Inline the codepoint-counted walk against sp_str_byte_len(s) instead so the result range can span an embedded NUL.

  • sp_utf8_byte_offset itself: same shape -- bound on sp_str_byte_len instead of "*p". For external 0xff string literals sp_str_byte_len falls back to strlen, preserving the pre-existing semantics.

Test: test/str_nul_bound_slice_bytes.rb.

Closes #657.

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

Details

Commit
d77a44b94cd2
Parent
7a3f615d994f

lib/sp_runtime.hmodifiedView file

@@ -502,19 +502,21 @@
502502 return off > sp_str_lcache[h].byte_len ? sp_str_lcache[h].byte_len : off;
503503 }
504504 }
505- /* Walk char_idx code points or stop at the NUL terminator, whichever comes
506- first. The loop body is safe at any *p != 0: sp_utf8_advance only reads
507- past p[0] when the continuation-byte mask matches, and NUL doesn't match,
508- so a truncated multibyte sequence near end-of-string still terminates. By
509- bounding on char_idx instead of byte_len, sp_str_sub_range's second
510- byte_offset call (on a mid-string pointer that doesn't sit in the cache)
511- no longer pays the strlen that sp_str_byte_len would otherwise charge for
512- a 0xff literal. */
505+ /* Walk char_idx code points or stop at byte_len, whichever comes first.
506+ Bounding on byte_len (instead of "*p != 0") keeps the walk correct
507+ past an embedded NUL byte -- a heap string with the 0xfe/0xfc marker
508+ carries its real length in the sp_str_hdr, so a NUL byte inside the
509+ payload no longer terminates the walk prematurely. For an external
510+ 0xff literal sp_str_byte_len falls back to strlen which legitimately
511+ stops at NUL, matching the prior behaviour. */
512+ size_t blen = sp_str_byte_len(s);
513513 const char *p = s;
514- while (char_idx > 0 && *p) {
514+ const char *end = s + blen;
515+ while (char_idx > 0 && p < end) {
515516 p += sp_utf8_advance(p);
516517 char_idx--;
517518 }
519+ if (p > end) p = end;
518520 return (size_t)(p - s);
519521 }
520522 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;}
@@ -1216,7 +1218,7 @@
12161218 static char sp_char_cache[256][3];
12171219 static int sp_char_cache_init = 0;
12181220 /* start/len are codepoint indices/counts. */
1219-static const char*sp_str_sub_range(const char*s,mrb_int start,mrb_int len){mrb_int cl=sp_str_length(s);if(start<0)start+=cl;if(start<0)start=0;if(start>=cl||len<=0){return &("\xff" "")[1];}if(start+len>cl)len=cl-start;size_t boff=sp_utf8_byte_offset(s,start);size_t bend=sp_utf8_byte_offset(s+boff,len)+boff;size_t blen=bend-boff;if(len==1&&blen==1){unsigned char c=(unsigned char)s[boff];if(!sp_char_cache_init){for(int i=0;i<256;i++){sp_char_cache[i][0]=(char)0xff;sp_char_cache[i][1]=(char)i;sp_char_cache[i][2]=0;}sp_char_cache_init=1;}return &sp_char_cache[c][1];}char*r=sp_str_alloc_raw(blen+1);memcpy(r,s+boff,blen);r[blen]=0;return r;}
1221+static const char*sp_str_sub_range(const char*s,mrb_int start,mrb_int len){mrb_int cl=sp_str_length(s);if(start<0)start+=cl;if(start<0)start=0;if(start>=cl||len<=0){return &("\xff" "")[1];}if(start+len>cl)len=cl-start;size_t boff=sp_utf8_byte_offset(s,start);size_t blen_total=sp_str_byte_len(s);size_t bp=boff;mrb_int rem=len;while(rem>0&&bp<blen_total){bp+=sp_utf8_advance(s+bp);rem--;}if(bp>blen_total)bp=blen_total;size_t bend=bp;size_t blen=bend-boff;if(len==1&&blen==1){unsigned char c=(unsigned char)s[boff];if(!sp_char_cache_init){for(int i=0;i<256;i++){sp_char_cache[i][0]=(char)0xff;sp_char_cache[i][1]=(char)i;sp_char_cache[i][2]=0;}sp_char_cache_init=1;}return &sp_char_cache[c][1];}char*r=sp_str_alloc_raw(blen+1);memcpy(r,s+boff,blen);r[blen]=0;return r;}
12201222 /* Single-character form of `s[i]`. Returns NULL on out-of-bounds to
12211223 match CRuby's `"hello"[20] -> nil`. The two-arg `s[i, len]` /
12221224 range forms keep returning "" on OOB via sp_str_sub_range; only
@@ -1225,7 +1227,7 @@
12251227 /* Char-indexed variant; the second arg used to be a hoisted byte length, now a
12261228 hoisted codepoint count. We don't need it for correctness, but keeping the
12271229 ABI lets callers pass it without a wrapper. */
1228-static const char*sp_str_sub_range_len(const char*s,mrb_int cl,mrb_int start,mrb_int len){if(start<0)start+=cl;if(start<0)start=0;if(start>=cl||len<=0){return &("\xff" "")[1];}if(start+len>cl)len=cl-start;size_t boff=sp_utf8_byte_offset(s,start);size_t bend=sp_utf8_byte_offset(s+boff,len)+boff;size_t blen=bend-boff;if(len==1&&blen==1){unsigned char c=(unsigned char)s[boff];if(!sp_char_cache_init){for(int i=0;i<256;i++){sp_char_cache[i][0]=(char)0xff;sp_char_cache[i][1]=(char)i;sp_char_cache[i][2]=0;}sp_char_cache_init=1;}return &sp_char_cache[c][1];}char*r=sp_str_alloc_raw(blen+1);memcpy(r,s+boff,blen);r[blen]=0;return r;}
1230+static const char*sp_str_sub_range_len(const char*s,mrb_int cl,mrb_int start,mrb_int len){if(start<0)start+=cl;if(start<0)start=0;if(start>=cl||len<=0){return &("\xff" "")[1];}if(start+len>cl)len=cl-start;size_t boff=sp_utf8_byte_offset(s,start);size_t blen_total=sp_str_byte_len(s);size_t bp=boff;mrb_int rem=len;while(rem>0&&bp<blen_total){bp+=sp_utf8_advance(s+bp);rem--;}if(bp>blen_total)bp=blen_total;size_t bend=bp;size_t blen=bend-boff;if(len==1&&blen==1){unsigned char c=(unsigned char)s[boff];if(!sp_char_cache_init){for(int i=0;i<256;i++){sp_char_cache[i][0]=(char)0xff;sp_char_cache[i][1]=(char)i;sp_char_cache[i][2]=0;}sp_char_cache_init=1;}return &sp_char_cache[c][1];}char*r=sp_str_alloc_raw(blen+1);memcpy(r,s+boff,blen);r[blen]=0;return r;}
12291231 /* String s[start..end] / s[start...end] with possibly negative
12301232 endpoints. Mirrors sp_IntArray_slice_range; issue #496. */
12311233 static const char*sp_str_sub_range_r(const char*s,mrb_int start,mrb_int end_,mrb_int excl){mrb_int cl=sp_str_length(s);if(end_<0)end_+=cl;mrb_int n=end_-start+(excl?0:1);if(n<0)n=0;return sp_str_sub_range_len(s,cl,start,n);}
@@ -1240,7 +1242,7 @@
12401242 static const char*sp_str_capitalize(const char*s){size_t l=strlen(s);char*r=sp_str_alloc_raw(l+1);for(size_t i=0;i<=l;i++)r[i]=tolower((unsigned char)s[i]);if(l>0)r[0]=toupper((unsigned char)r[0]);return r;}
12411243 static mrb_int sp_str_count(const char*s,const char*chars){if(!chars)return 0;size_t setn;uint32_t*set=sp_utf8_decode_all(chars,&setn);mrb_int c=0;const char*p=s;while(*p){uint32_t cp;p+=sp_utf8_decode(p,&cp);if(sp_utf8_set_has(set,setn,cp))c++;}free(set);return c;}
12421244 static const char*sp_str_repeat(const char*s,mrb_int n){if(n<=0)return sp_str_empty;size_t l=strlen(s);char*r=sp_str_alloc_raw(l*n+1);for(mrb_int i=0;i<n;i++)memcpy(r+l*i,s,l);r[l*n]=0;return r;}
1243-static sp_IntArray*sp_str_bytes(const char*s){sp_IntArray*a=sp_IntArray_new();for(size_t i=0;s[i];i++)sp_IntArray_push(a,(mrb_int)(unsigned char)s[i]);return a;}
1245+static sp_IntArray*sp_str_bytes(const char*s){sp_IntArray*a=sp_IntArray_new();if(!s)return a;size_t n=sp_str_byte_len(s);for(size_t i=0;i<n;i++)sp_IntArray_push(a,(mrb_int)(unsigned char)s[i]);return a;}
12441246 static const char*sp_str_tr(const char*s,const char*from,const char*to){size_t fn,tn;uint32_t*fcps=sp_utf8_decode_all(from,&fn);uint32_t*tcps=sp_utf8_decode_all(to,&tn);size_t bl=strlen(s);size_t cap=bl*4+1;char*buf=(char*)malloc(cap);size_t n=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);size_t mi=fn;for(size_t j=0;j<fn;j++)if(fcps[j]==cp){mi=j;break;}if(mi<fn&&tn>0){uint32_t rep=mi<tn?tcps[mi]:tcps[tn-1];n+=sp_utf8_encode(rep,buf+n);}else{memcpy(buf+n,p,cn);n+=cn;}p+=cn;}buf[n]=0;char*r=sp_str_alloc(n);memcpy(r,buf,n+1);free(buf);free(fcps);free(tcps);return r;}
12451247 static const char*sp_str_delete(const char*s,const char*chars){if(!chars)return s;size_t setn;uint32_t*set=sp_utf8_decode_all(chars,&setn);size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t n=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);if(!sp_utf8_set_has(set,setn,cp)){memcpy(r+n,p,cn);n+=cn;}p+=cn;}r[n]=0;free(set);return r;}
12461248 static const char*sp_str_squeeze(const char*s){size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t n=0;uint32_t prev=0xFFFFFFFFu;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);if(cp!=prev){memcpy(r+n,p,cn);n+=cn;prev=cp;}p+=cn;}r[n]=0;return r;}