Skip to content

Use String#undump instead of eval - #333

Merged
hsbt merged 1 commit into
ruby:masterfrom
kou:undump
Aug 27, 2026
Merged

Use String#undump instead of eval#333
hsbt merged 1 commit into
ruby:masterfrom
kou:undump

Conversation

@kou

@kou kou commented Jul 10, 2026

Copy link
Copy Markdown
Member

String#undump is enough to parse a string literal.

Copilot AI review requested due to automatic review settings July 10, 2026 05:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Racc’s grammar file lexer (GrammarFileParser#yylex0) to avoid eval when parsing quoted string literals, aiming to reduce code execution risk by using String#undump instead.

Changes:

  • Replaces eval(scan_quoted(...)) with String#undump for string-literal parsing.
  • Adds a conversion step intended to make single-quoted literals compatible with undump.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/racc/grammarfileparser.rb Outdated
@hsbt

hsbt commented Jul 28, 2026

Copy link
Copy Markdown
Member

Grammar files that use raw non-ASCII characters in double-quoted string tokens (e.g. "あ") now raise RuntimeError: non-ASCII character detected, because String#undump only accepts ASCII input. Escaping non-ASCII characters to \u{...} before calling undump avoids this:

string = string_literal.gsub(/[^\x00-\x7F]/) { |c| "\\u{%x}" % c.ord }.undump

undump decodes \u escapes back to UTF-8, so the result is the same as eval for UTF-8 sources.

String#undump is enough to parse a string literal.
@kou

kou commented Aug 13, 2026

Copy link
Copy Markdown
Member Author

Good point. I didn't notice it.

string_literal is ASCII-8BIT here because String#b is used in #initialize. And input may not be UTF-8. So I used \xHH not \uHHHH.

@hsbt
hsbt merged commit 4d858d9 into ruby:master Aug 27, 2026
33 checks passed
@kou
kou deleted the undump branch August 27, 2026 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants