Skip to content

Commit

Permalink
+ lexer.rl: reject \u after control/meta escape chars. (#807)
Browse files Browse the repository at this point in the history
This commit tracks upstream commit ruby/ruby@110f242.
  • Loading branch information
iliabylich committed Jul 8, 2021
1 parent 7ecddb6 commit 9b24e6a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/parser/lexer.rl
Expand Up @@ -705,6 +705,11 @@ class Parser::Lexer

action unescape_char {
codepoint = @source_pts[p - 1]

if @version >= 30 && (codepoint == 117 || codepoint == 85) # 'u' or 'U'
diagnostic :fatal, :invalid_escape
end

if (@escape = ESCAPES[codepoint]).nil?
@escape = encode_escape(@source_buffer.slice(p - 1))
end
Expand Down
40 changes: 40 additions & 0 deletions test/test_lexer.rb
Expand Up @@ -3614,4 +3614,44 @@ def refute_scanned_numbered_parameter(input, message = nil)
end
end

def test_meta_escape_slash_u__before_30
setup_lexer(27)
assert_scanned('"\c\u0000"',
:tSTRING, "\u00150000", [0, 10])
assert_scanned('"\c\U0000"',
:tSTRING, "\u00150000", [0, 10])

assert_scanned('"\C-\u0000"',
:tSTRING, "\u00150000", [0, 11])
assert_scanned('"\C-\U0000"',
:tSTRING, "\u00150000", [0, 11])

assert_scanned('"\M-\u0000"',
:tSTRING, "\xF50000", [0, 11])
assert_scanned('"\M-\U0000"',
:tSTRING, "\xD50000", [0, 11])
end

def refute_scanned_meta_escape_slash_u(input)
setup_lexer(30)
source_buffer = Parser::Source::Buffer.new('(refute_scanned_meta_escape_slash_u)', source: input)
@lex.source_buffer = source_buffer

err = assert_raises Parser::SyntaxError do
@lex.advance
end

assert_equal :invalid_escape, err.diagnostic.reason
end

def test_meta_escape_slash_u__after_30
refute_scanned_meta_escape_slash_u('"\c\u0000"')
refute_scanned_meta_escape_slash_u('"\c\U0000"')

refute_scanned_meta_escape_slash_u('"\C-\u0000"')
refute_scanned_meta_escape_slash_u('"\C-\U0000"')

refute_scanned_meta_escape_slash_u('"\M-\u0000"')
refute_scanned_meta_escape_slash_u('"\M-\U0000"')
end
end

0 comments on commit 9b24e6a

Please sign in to comment.