Commit 5cdb7ba3 authored by Stefan Behnel's avatar Stefan Behnel

reject invalid hex/unicode escape sequences instead of letting them crash the compiler

parent 7f45b0b7
...@@ -782,13 +782,19 @@ def p_string_literal(s, kind_override=None): ...@@ -782,13 +782,19 @@ def p_string_literal(s, kind_override=None):
elif c == u'\n': elif c == u'\n':
pass pass
elif c == u'x': elif c == u'x':
chars.append_charval( int(systr[2:], 16) ) if len(systr) == 4:
chars.append_charval( int(systr[2:], 16) )
else:
s.error("Invalid hex escape '%s'" % systr, pos=s.position())
elif c in u'Uu': elif c in u'Uu':
if kind in ('u', ''): if kind in ('u', ''):
chrval = int(systr[2:], 16) if len(systr) in (6,10):
if chrval > 1114111: # sys.maxunicode: chrval = int(systr[2:], 16)
s.error("Invalid unicode escape '%s'" % systr, if chrval > 1114111: # sys.maxunicode:
pos = pos) s.error("Invalid unicode escape '%s'" % systr,
pos = pos)
else:
s.error("Invalid unicode escape '%s'" % systr, pos=s.position())
else: else:
# unicode escapes in plain byte strings are not unescaped # unicode escapes in plain byte strings are not unescaped
chrval = None chrval = None
......
'\x'
_ERRORS = '''
2:1: Invalid hex escape '\x'
'''
'\x1'
_ERRORS = '''
2:1: Invalid hex escape '\x'
'''
u'\uXYZ'
_ERRORS = '''
2:2: Invalid unicode escape '\u'
'''
u'\u'
_ERRORS = '''
2:1: Invalid unicode escape '\u'
'''
u'\u12'
_ERRORS = '''
2:1: Invalid unicode escape '\u'
'''
...@@ -125,6 +125,13 @@ __doc__ = ur""" ...@@ -125,6 +125,13 @@ __doc__ = ur"""
>>> len(uresc) >>> len(uresc)
9 9
>>> bytes_uescape
b'\\u1234\\U12345678\\u\\u1\\u12\\uX'
>>> bytes_uescape == b'\\u1234\\U12345678\\u\\u1\\u12\\uX'
True
>>> len(bytes_uescape)
28
>>> newlines == "Aaa\n" >>> newlines == "Aaa\n"
True True
...@@ -165,6 +172,8 @@ sresc = r'\12\'\"\\' ...@@ -165,6 +172,8 @@ sresc = r'\12\'\"\\'
bresc = br'\12\'\"\\' bresc = br'\12\'\"\\'
uresc = ur'\12\'\"\\' uresc = ur'\12\'\"\\'
bytes_uescape = b'\u1234\U12345678\u\u1\u12\uX'
newlines = "Aaa\n" newlines = "Aaa\n"
# T640, long literals with escapes # T640, long literals with escapes
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment