Bug #22560
closedparse.y hits UNREACHABLE_RETURN(0) after compile_error in rb_parser_enc_cr_str_buf_cat
Description
In CRuby's parse.y, rb_parser_enc_cr_str_buf_cat calls non-fatal compile_error(p, "incompatible character encodings: ...") (parser_compile_error / ripper_compile_error) followed immediately by UNREACHABLE_RETURN(0) (__builtin_unreachable()). Because compile_error appends the syntax error and returns normally rather than longjmping, control flow falls off __builtin_unreachable() into undefined code.
This is reliably triggered by tokadd_utf8 / tokadd_codepoint passing a hardcoded rb_utf8_encoding() instead of p->enc to flush_string_content on an invalid or unterminated \u{...} escape inside an ASCII-8BIT string literal spanning a newline (p->delayed.token).
ruby -e 'STDOUT.binmode; STDOUT.write("#coding:ascii-8bit\n\"\x80\n\x80\\u{0")' > foo.rb
✗ lldb ruby
(lldb) target create "ruby"
Current executable set to '/opt/homebrew/opt/ruby/bin/ruby' (arm64).
(lldb) run --parser=parse.y foo.rb
Process 19325 launched: '/opt/homebrew/opt/ruby/bin/ruby' (arm64)
warning: (arm64) /opt/homebrew/Cellar/ruby/4.0.9/lib/ruby/4.0.0/arm64-darwin27/enc/encdb.bundle empty dSYM file detected, dSYM was created with an executable with no debug info.
warning: (arm64) /opt/homebrew/Cellar/ruby/4.0.9/lib/ruby/4.0.0/arm64-darwin27/enc/trans/transdb.bundle empty dSYM file detected, dSYM was created with an executable with no debug info.
warning: (arm64) /opt/homebrew/Cellar/ruby/4.0.9/lib/ruby/4.0.0/arm64-darwin27/monitor.bundle empty dSYM file detected, dSYM was created with an executable with no debug info.
Process 19325 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x1009eae78)
frame #0: 0x00000001009eae78 libruby.4.0.dylib`rb_parser_enc_cr_str_buf_cat + 432
libruby.4.0.dylib`rb_parser_enc_cr_str_buf_cat:
-> 0x1009eae78 <+432>: brk #0x1
libruby.4.0.dylib`parser_yylex:
0x1009eae7c <+0>: sub sp, sp, #0xf0
0x1009eae80 <+4>: stp x28, x27, [sp, #0x90]
0x1009eae84 <+8>: stp x26, x25, [sp, #0xa0]
Target 0: (ruby) stopped.
Updated by nobu (Nobuyoshi Nakada) 1 day ago
- Status changed from Open to Closed
- Backport changed from 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN to 3.3: DONTNEED, 3.4: REQUIRED, 4.0: REQUIRED
Fixed by https://github.com/ruby/ruby/pull/19936.
Updated by k0kubun (Takashi Kokubun) about 11 hours ago
- Backport changed from 3.3: DONTNEED, 3.4: REQUIRED, 4.0: REQUIRED to 3.3: DONTNEED, 3.4: REQUIRED, 4.0: DONE
ruby_4_0 69f956b7ce94eaf84d3afed68ff1d9b871265d9d merged revision(s) ebdf919795c0983696023691177045014435551c.