Commit 98e5f804
98e5f804d223Merged
Ori Pekelman <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(codegen): String#index(sub, start) honours start offset
The 2-arg form was silently dropping start: the codegen branch
emitted sp_str_index(rc, compile_arg0(nid)) regardless of arity,
so
"a.b.c".index(".", 2)
returned 1 (the first dot) instead of 3. The natural "walk all matches" loop never advanced past the first hit.
Fix
-
New runtime helper
sp_str_index_from(s, sub, start)insp_runtime.h. Codepoint-indexed (matches the 1-argsp_str_indexcontract), supports negative start (clamps to 0 when too negative; out-of-range positive returns -1). -
Codegen peels off the 2-arg case before falling through to the existing 1-arg path.
Coverage
test/string_index_from.rb:
- 1-arg baseline.
- 2-arg with start at 0, on the dot itself, just past it, in/out of range.
- Negative start (back from end), oversize-negative (clamp).
- Out-of-range positive start.
- Multibyte string (codepoint, not byte indexing).
- The "walk all dots" loop pattern that surfaced this bug.
Where this came up
JWT parsing in tep needed both . separators in a token:
dot1 = token.index(".")
dot2 = token.index(".", dot1 + 1) # always returned dot1 -- bug
Pre-fix the verifier's signing_input was always just the
header (no payload), the HMAC didn't match, and every token
verified as "bad signature". The hand-rolled find_dots
workaround in Tep::Jwt can drop after this lands.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 98e5f804d223
- Parent
- 9daca9d7db76
lib/sp_runtime.hmodifiedView file
static const char*sp_str_gsub(const char*s,const char*pat,const char*rep){size_t pl=strlen(pat),rl=strlen(rep),sl=strlen(s);if(pl==0)return s;size_t cap=sl*2+1;char*out=(char*)malloc(cap);size_t ol=0;const char*p=s;while(*p){const char*f=strstr(p,pat);if(!f){size_t n=strlen(p);if(ol+n>=cap){cap=(ol+n)*2+1;out=(char*)realloc(out,cap);}memcpy(out+ol,p,n);ol+=n;break;}size_t n=f-p;if(ol+n+rl>=cap){cap=(ol+n+rl)*2+1;out=(char*)realloc(out,cap);}memcpy(out+ol,p,n);ol+=n;memcpy(out+ol,rep,rl);ol+=rl;p=f+pl;}out[ol]=0;return out;}/* Returns a *character* offset (codepoint index), not a byte offset. */static mrb_int sp_str_index(const char*s,const char*sub){const char*f=strstr(s,sub);if(!f)return -1;mrb_int n=0;const char*p=s;while(p<f){p+=sp_utf8_advance(p);n++;}return n;}/* `s.index(sub, start)` -- search starts at codepoint index `start`. Negative start counts back from the end of the string. Returns the absolute codepoint offset of the match (not relative to start), or -1 when no match exists at or after start. Out-of-range starts (start > length) return -1, matching CRuby. */static mrb_int sp_str_index_from(const char*s,const char*sub,mrb_int start){mrb_int cl=sp_str_length(s);if(start<0)start+=cl;if(start<0)start=0;if(start>cl)return -1;size_t boff=sp_utf8_byte_offset(s,start);const char*f=strstr(s+boff,sub);if(!f)return -1;mrb_int n=0;const char*p=s;while(p<f){p+=sp_utf8_advance(p);n++;}return n;}static mrb_int sp_str_rindex(const char*s,const char*sub){size_t sl=strlen(sub);if(sl==0)return sp_str_length(s);const char*last=NULL;const char*p=s;while((p=strstr(p,sub))){last=p;p++;}if(!last)return -1;mrb_int n=0;const char*q=s;while(q<last){q+=sp_utf8_advance(q);n++;}return n;}static char sp_char_cache[256][3];static int sp_char_cache_init = 0;spinel_codegen.rbmodifiedView file
Loading diff…
test/string_index_from.rbaddedView file
Loading diff…
test/string_index_from.rb.expectedaddedView file
Loading diff…