Skip to content

Commit f1b8b1b

Browse files
committed
Do not leak explicit encoding
Fixes [Bug #20744]
1 parent f544151 commit f1b8b1b

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed

lib/prism/translation/ruby_parser.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,13 @@ def visit_statements_node(node)
14281428
# "foo"
14291429
# ^^^^^
14301430
def visit_string_node(node)
1431-
s(node, :str, node.unescaped)
1431+
unescaped = node.unescaped
1432+
1433+
if node.forced_binary_encoding?
1434+
unescaped.force_encoding(Encoding::BINARY)
1435+
end
1436+
1437+
s(node, :str, unescaped)
14321438
end
14331439

14341440
# super(foo)

src/prism.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ lex_mode_push_regexp(pm_parser_t *parser, uint8_t incrementor, uint8_t terminato
168168
breakpoints[index++] = incrementor;
169169
}
170170

171+
parser->explicit_encoding = NULL;
171172
return lex_mode_push(parser, lex_mode);
172173
}
173174

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# encoding: US-ASCII
2+
str = "hello \xFC"
3+
str =~ /hello \u{fc}/

test/prism/locals_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
# to test on the most recent versions.
1010
return if !defined?(RubyVM::InstructionSequence) || RUBY_VERSION < "3.4.0"
1111

12+
# If we're on Ruby 3.4.0 and the default parser is Prism, then there is no point
13+
# in comparing the locals because they will be the same.
14+
return if RubyVM::InstructionSequence.compile("").to_a[4][:parser] == :prism
15+
1216
# In Ruby 3.4.0, the local table for method forwarding changed. But 3.4.0 can
1317
# refer to the dev version, so while 3.4.0 still isn't released, we need to
1418
# check if we have a high enough revision.

test/prism/snapshots/regex_escape_encoding.txt

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/prism/test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def self.each_encoding
209209

210210
private
211211

212-
if RUBY_ENGINE == "ruby"
212+
if RUBY_ENGINE == "ruby" && RubyVM::InstructionSequence.compile("").to_a[4][:parser] != :prism
213213
# Check that the given source is valid syntax by compiling it with RubyVM.
214214
def check_syntax(source)
215215
ignore_warnings { RubyVM::InstructionSequence.compile(source) }

0 commit comments

Comments
 (0)