Commit 880d7270
880d7270f2c0Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 9 files · +2 added · ~7 modified
Description
fix(analyze,codegen,runtime): String#index returns nil for not-found
String#index / String#rindex previously returned mrb_int
with -1 as the not-found sentinel. Per #521's strict
int == nil -> FALSE, the CRuby idiom
pos = body.index('"content"', i)
break if pos.nil?
never exited the loop because pos.nil? on a plain-int local is
always false. Ori Pekelman hit this writing a GPT-2 BPE token
scanner that walks JSON content fields; the result was an
infinite loop wandering past the buffer and Bus-erroring.
This change widens String#index / String#rindex's return to sp_RbVal -- boxed nil for the -1 sentinel, boxed int for found. CRuby semantics now work end-to-end:
pos.nil?/pos == nil/pos != nilvia the standard poly-tag dispatch.pos.inspectvia sp_poly_inspect (prints "nil" or the int).while pos = s.index(...)truthiness (boxed nil is falsy).
Implementation:
- lib/sp_runtime.h: new
sp_str_index_poly/sp_str_index_from_poly/sp_str_rindex_poly/sp_re_rindex_polywrappers over the existing mrb_int-returning helpers, boxing the result. - spinel_codegen.rb:
compile_str_method_expr'sindex/rindexarms emit the _poly variants;compile_poly_method_callgains aninspectarm via sp_poly_inspect; new helpercompile_expr_as_int(nid)unboxes via.v.iwhen the static type is poly, used at the int-expecting C helper boundaries (sp_str_sub_range, the string[]two-arg form, sp_str_index_from's start arg). - spinel_analyze.rb:
String#index/#rindexinfer as "poly" when the receiver isstring/mutable_str. Array#index / #find_index intentionally keep the int return for now -- the same structural fix applies but #532's reproduction surface is strings, and widening Array#index cascades through more call sites that consume the result as a raw index.
Test fixture updates:
- test/string_index_from.rb: rewritten to the new idiom -- the
walk-all-dots loop now uses
break if pos.nil?instead ofpos < 0, andpos.to_i + 1unbox at the int-arithmetic site. Expected file: the four "-1" lines that documented the prior broken behavior become empty lines (puts nil.to_s->""). - test/str_method_nil_arg_no_segv.rb.expected:
-1line becomesnilforp "foo".rindex(/missing/). The test's inline comment already documented# CRuby: nil. spinel: -1; the spinel output now matches CRuby. - test/string_index_nullable.rb (new): exercises the full
nil-lens shape -- found/not-found
.inspect,.nil?, the walk-all loop pattern from the issue.
This is option 1 from the #532 design comment; the user
explicitly accepted the boxing/unbox cost ("trust future-me to
handle nullable well"). The compile_expr_as_int helper handles
the bootstrap's own name.index("_cls_") shape and the test
fixtures' integer-index uses; future widenings (Array#index, etc.)
can extend the same pattern.
Closes #532
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 880d7270f2c0
- Parent
- c3e7e0a8d3ff
lib/sp_runtime.hmodifiedView file
when non-NULL. */static inline mrb_int sp_obj_cls_id_of(void *p) { return p ? *(mrb_int *)p : 0; }static sp_RbVal sp_box_nullable_str(const char *v) { return v ? sp_box_str(v) : sp_box_nil(); }/* String#index / #rindex return a boxed nil for not-found, boxed int for found. Issue #532: typed-int slot can't represent CRuby's nil-vs-real-index distinction in-band; widening the result type to sp_RbVal at the call site lets `pos.nil?` and `puts pos.inspect` work via the standard poly-tag dispatch. The -1 sentinel comes from the underlying sp_str_*_index helpers; we widen here at the boxing layer so existing call sites that want the raw int still work via `sp_str_index` directly. */static sp_RbVal sp_str_index_poly(const char *s, const char *sub) { mrb_int n = sp_str_index(s, sub); return n < 0 ? sp_box_nil() : sp_box_int(n); }static sp_RbVal sp_str_index_from_poly(const char *s, const char *sub, mrb_int start) { mrb_int n = sp_str_index_from(s, sub, start); return n < 0 ? sp_box_nil() : sp_box_int(n); }static sp_RbVal sp_str_rindex_poly(const char *s, const char *sub) { mrb_int n = sp_str_rindex(s, sub); return n < 0 ? sp_box_nil() : sp_box_int(n); }static sp_RbVal sp_re_rindex_poly(mrb_regexp_pattern *pat, const char *str) { mrb_int n = sp_re_rindex(pat, str); return n < 0 ? sp_box_nil() : sp_box_int(n); }static sp_RbVal sp_box_nullable_obj(void *p, int cls_id) { return p ? sp_box_obj(p, cls_id) : sp_box_nil(); }/* Built-in pointer boxes — share SP_TAG_OBJ with a reserved negative cls_id so the dispatch path is uniform. */spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/str_method_nil_arg_no_segv.rbmodifiedView file
Loading diff…
test/str_method_nil_arg_no_segv.rb.expectedmodifiedView file
Loading diff…
test/string_index_from.rbmodifiedView file
Loading diff…
test/string_index_from.rb.expectedmodifiedView file
Loading diff…
test/string_index_nullable.rbaddedView file
Loading diff…
test/string_index_nullable.rb.expectedaddedView file
Loading diff…