Commit 58a4fb70

58a4fb703932Merged

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

Description

Return false for a non-String argument to a String Range#===

('A'..'C') === 20 segfaulted: the String-range cover/include?/=== arm checked that the range bounds were strings but not the argument, so an integer argument lowered to 20LL and was passed to strcmp as a pointer. A non-String value is never inside a String range, so return false (CRuby does the same rather than raising). String arguments and numeric ranges are unchanged.

Fixes #1313

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

Details

Commit
58a4fb703932
Parent
b3d536453a1a

spinel_codegen.rbmodifiedView file

@@ -23048,6 +23048,19 @@
2304823048 l_nid = @nd_left[lit_rg]
2304923049 r_nid = @nd_right[lit_rg]
2305023050 if l_nid >= 0 && r_nid >= 0 && infer_type(l_nid) == "string" && infer_type(r_nid) == "string"
23051+ # A non-string argument is never inside a string range: CRuby returns
23052+ # false rather than raising. Without this guard the argument lowers to a
23053+ # non-pointer (e.g. `20LL`) that is fed to strcmp, which segfaults.
23054+ a0_rng = -1
23055+ if @nd_arguments[nid] >= 0
23056+ a0s_rng = get_args(@nd_arguments[nid])
23057+ if a0s_rng.length >= 1
23058+ a0_rng = a0s_rng[0]
23059+ end
23060+ end
23061+ if a0_rng >= 0 && base_type(infer_type(a0_rng)) != "string"
23062+ return "FALSE"
23063+ end
2305123064 arg_s = compile_arg0(nid)
2305223065 left_s = compile_expr(l_nid)
2305323066 right_s = compile_expr(r_nid)