Commit b8fce9e9
b8fce9e990cfMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Raise TypeError for Regexp match on a non-String operand
/re/ =~ 5 (and match?/match/=== with a non-String argument)
segfaulted: the scalar was passed where sp_re_match* expects a
const char*, so an integer was dereferenced as a pointer.
CRuby raises TypeError ("no implicit conversion of Integer into String"). Guard the regex-match dispatch: when the operand's static type is a non-String scalar (int/bigint/float/symbol/nil/bool), emit sp_raise_cls("TypeError", ...) with a type-matched message, returning a type-correct dummy per method (FALSE for match?/===, nil for =~/match) so the abort path still type-checks.
/p/ =~ 5 # => TypeError (was: segfault) /l/ =~ "hello" # => 2 (unchanged) /zz/ =~ "hello" # => nil (unchanged)
Fixes #1169
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- b8fce9e990cf
- Parent
- 6219131053d8
spinel_codegen.rbmodifiedView file
arg_ids = get_args(args_id) if arg_ids.length > 0 sc = compile_expr(arg_ids[0]) # A non-String match operand can't be coerced: CRuby raises # TypeError. Without this guard the scalar is passed where a # const char* is expected (sp_re_match*), so the integer is # dereferenced as a pointer and segfaults. sc_bt_re = base_type(infer_type(arg_ids[0])) if sc_bt_re == "int" || sc_bt_re == "bigint" || sc_bt_re == "float" || sc_bt_re == "symbol" || sc_bt_re == "nil" || sc_bt_re == "bool" rb_t_re = "Integer" if sc_bt_re == "float" rb_t_re = "Float" elsif sc_bt_re == "symbol" rb_t_re = "Symbol" elsif sc_bt_re == "nil" rb_t_re = "nil" elsif sc_bt_re == "bool" rb_t_re = "true" end raise_re = "sp_raise_cls(\"TypeError\", \"no implicit conversion of " + rb_t_re + " into String\")" if mname == "match?" || mname == "===" return "(" + raise_re + ", FALSE)" end @needs_rb_value = 1 return "(" + raise_re + ", sp_box_nil())" end if mname == "match?" || mname == "===" # Issue #869: 2-arg form starts matching at the given byte # offset (negative counts from the end). Without the _attest/regex_match_nonstring.rbaddedView file
Loading diff…
test/regex_match_nonstring.rb.expectedaddedView file
Loading diff…