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

@@ -17234,6 +17234,29 @@
1723417234 arg_ids = get_args(args_id)
1723517235 if arg_ids.length > 0
1723617236 sc = compile_expr(arg_ids[0])
17237+ # A non-String match operand can't be coerced: CRuby raises
17238+ # TypeError. Without this guard the scalar is passed where a
17239+ # const char* is expected (sp_re_match*), so the integer is
17240+ # dereferenced as a pointer and segfaults.
17241+ sc_bt_re = base_type(infer_type(arg_ids[0]))
17242+ 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"
17243+ rb_t_re = "Integer"
17244+ if sc_bt_re == "float"
17245+ rb_t_re = "Float"
17246+ elsif sc_bt_re == "symbol"
17247+ rb_t_re = "Symbol"
17248+ elsif sc_bt_re == "nil"
17249+ rb_t_re = "nil"
17250+ elsif sc_bt_re == "bool"
17251+ rb_t_re = "true"
17252+ end
17253+ raise_re = "sp_raise_cls(\"TypeError\", \"no implicit conversion of " + rb_t_re + " into String\")"
17254+ if mname == "match?" || mname == "==="
17255+ return "(" + raise_re + ", FALSE)"
17256+ end
17257+ @needs_rb_value = 1
17258+ return "(" + raise_re + ", sp_box_nil())"
17259+ end
1723717260 if mname == "match?" || mname == "==="
1723817261 # Issue #869: 2-arg form starts matching at the given byte
1723917262 # offset (negative counts from the end). Without the _at