Skip to content

Fuzzing finds #4526

Done
Done

Description

@stevenjohnstone

New findings from differential (parse.y vs Prism) fuzzing against upstream Prism 12fe57bda488ce53c7a71786d4dde884ea186980 / CRuby ruby 4.1.0dev (2026-12-02T14:21:27Z HEAD 4ff592f0ab) +PRISM.

Runtime / Semantic Discrepancies

  • Heredoc concatenated with string literal skips mixed-encoding check and produces corrupted bytes

    Reproduction
    ruby -e 'print "# coding: Windows-31J\np <<_H\"\\xa2\"\n\#{?\\u3042}\n_H\n"' | ruby --parser=parse.y --disable-gems
    ruby -e 'print "# coding: Windows-31J\np <<_H\"\\xa2\"\n\#{?\\u3042}\n_H\n"' | ruby --parser=prism   --disable-gems
    • parse.y: SyntaxError: string literal encodings differ (UTF-8 / Windows-31J)
    • prism: prints corrupted mojibake "\x{E381}\x82\n\xA2" (exit 0; inline "#{?\u3042}""\xa2" is properly rejected by Prism)
  • Mixed string encodings inside interpolated concatenated string literal accepted at parse time, fails at runtime (Encoding::CompatibilityError)

    Reproduction
    ruby -e 'print "# coding: US-ASCII\n\"\\xcd\#{\"\\u3042\"\"\"}\"\n"' | ruby --parser=parse.y --disable-gems -c
    ruby -e 'print "# coding: US-ASCII\n\"\\xcd\#{\"\\u3042\"\"\"}\"\n"' | ruby --parser=prism   --disable-gems -c
    • parse.y: SyntaxError: string literal encodings differ (ASCII-8BIT / UTF-8)
    • prism: Syntax OK (fails at runtime with Encoding::CompatibilityError; without string concatenation, "\xcd#{"\u3042"}" is rejected by both parsers)
  • defined?(class B; yield; end) and defined?(module B; yield; end) rejected with Invalid yield

    Reproduction
    ruby --parser=parse.y --disable-gems -e 'p defined?(class B; yield; end); p defined?(module B; yield; end)'
    ruby --parser=prism   --disable-gems -e 'p defined?(class B; yield; end); p defined?(module B; yield; end)'
    • parse.y: "expression", "expression" (exit 0)
    • prism: SyntaxError: Invalid yield (bare defined?(yield) returns nil in both)

Syntax Acceptance / Rejection Discrepancies

  • e case a; in d; else if d; else t m (){} do end end end rejected (unexpected 'do'; regressed in 55d9f0e027)

    Reproduction
    ruby --parser=parse.y --disable-gems -ce 'e case a; in d; else if d; else t m (){} do end end end'
    ruby --parser=prism   --disable-gems -ce 'e case a; in d; else if d; else t m (){} do end end end'
    • parse.y: Syntax OK
    • prism: SyntaxError: unexpected 'do', ignoring it (without the inner if ... else, e case a; in d; else t m (){} do end end is accepted by both)
    • Bisected to 55d9f0e027a84d4ee6001274a243fe6a96f3838a (Parser reductions #4475)
  • Newline after keyword parameter label inside def in case ... in ... if guard rejected (regressed in d8d9908a14)

    Reproduction
    ruby -e 'print "case i\nin [] if def p(b:\n) end\nend\n"' | ruby --parser=parse.y --disable-gems -c
    ruby -e 'print "case i\nin [] if def p(b:\n) end\nend\n"' | ruby --parser=prism   --disable-gems -c
    • parse.y: Syntax OK
    • prism: SyntaxError: unexpected newline; expected a ')' to close the parameters
    • Bisected to d8d9908a14824539307d1c54a8442be6d0317552 (Fix label newline handling in patterns and bare method parameters)
  • Non-ASCII byte-escaped hash-pattern shorthand key ({"\xcd":}) rejected under # coding: US-ASCII

    Reproduction
    ruby -e 'print "# coding: US-ASCII\ncase {}; in {\"\\xcd\":}; end\n"' | ruby --parser=parse.y --disable-gems -c
    ruby -e 'print "# coding: US-ASCII\ncase {}; in {\"\\xcd\":}; end\n"' | ruby --parser=prism   --disable-gems -c
    • parse.y: Syntax OK (under # coding: binary, both parsers accept it)
    • prism: SyntaxError: key must be valid as local variables
  • # shareable_constant_value: literal allows and/or after bare splat constant assignment (X = *b and X)

    Reproduction
    ruby -e 'print "# shareable_constant_value: literal\nX = *b and X\n"' | ruby --parser=parse.y --disable-gems -c
    ruby -e 'print "# shareable_constant_value: literal\nX = *b and X\n"' | ruby --parser=prism   --disable-gems -c
    • parse.y: SyntaxError: syntax error, unexpected 'and', expecting end-of-input (same as without the magic comment)
    • prism: Syntax OK (without # shareable_constant_value: literal, Prism rejects X = *b and X identically to parse.y)
  • Heredoc followed on same line by multiline quoted hash label key fails to parse

    Reproduction
    ruby -e "print \"p(<<A, {'\nA\nx': 1})\n\"" | ruby --parser=parse.y --disable-gems -c
    ruby -e "print \"p(<<A, {'\nA\nx': 1})\n\"" | ruby --parser=prism   --disable-gems -c
    • parse.y: Syntax OK
    • prism: SyntaxError: expected a '}' to close the hash literal / unterminated string meets end of file (replacing : with => is accepted by both)
  • for j in (break if nil); end rejected with Invalid break

    Reproduction
    ruby --parser=parse.y --disable-gems -ce 'for j in (break if nil); end'
    ruby --parser=prism   --disable-gems -ce 'for j in (break if nil); end'
    • parse.y: Syntax OK (top-level break if nil is rejected by both)
    • prism: SyntaxError: Invalid break

All-in-one verification script
run_pair() {
  local num="$1" mode="$2" title="$3"
  local tmp; tmp="$(mktemp)"
  cat > "$tmp"
  echo "==================== #$num: $title ===================="
  echo "--- parse.y ---"
  ruby --parser=parse.y --disable-gems $mode "$tmp" 2>&1; echo "(exit=$?)"
  echo "--- prism ---"
  ruby --parser=prism   --disable-gems $mode "$tmp" 2>&1; echo "(exit=$?)"
  rm -f "$tmp"
}

run_pair 1 "" "Heredoc concatenated with string literal skips mixed-encoding check" <<'RUBY'
# coding: Windows-31J
p <<_H"\xa2"
#{?\u3042}
_H
RUBY

run_pair 2 -c "Mixed string encodings inside interpolated concatenated string" <<'RUBY'
# coding: US-ASCII
"\xcd#{"\u3042"""}"
RUBY

run_pair 3 "" "defined?(class B; yield; end) and defined?(module B; yield; end)" <<'RUBY'
p defined?(class B; yield; end)
p defined?(module B; yield; end)
RUBY

run_pair 4 -c "e case a; in d; else if d; else t m (){} do end end end" <<'RUBY'
e case a; in d; else if d; else t m (){} do end end end
RUBY

run_pair 5 -c "Newline after keyword parameter label inside def in case..in..if guard" <<'RUBY'
case i
in [] if def p(b:
) end
end
RUBY

run_pair 6 -c "Non-ASCII byte-escaped hash-pattern shorthand key under US-ASCII" <<'RUBY'
# coding: US-ASCII
case {}; in {"\xcd":}; end
RUBY

run_pair 7 -c "shareable_constant_value: literal allows and/or after bare splat constant assignment" <<'RUBY'
# shareable_constant_value: literal
X = *b and X
RUBY

run_pair 8 -c "Heredoc followed on same line by multiline quoted hash label key" <<'RUBY'
p(<<A, {'
A
x': 1})
RUBY

run_pair 9 -c "for j in (break if nil); end" <<'RUBY'
for j in (break if nil); end
RUBY

Activity

  1. kddnewton commented on Dec 2, 2026

    @kddnewton
    Collaborator

    That non-ascii shorthand thing I'm not going to fix until I get a ruling on https://bugs.ruby-lang.org/issues/22663. I think that should be a syntax error.

  2. kddnewton commented on Dec 2, 2026

    @kddnewton
    Collaborator

    The for loop thing should be fixed upstream by ruby/ruby#20313

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions