diff options
-rw-r--r-- | mesonbuild/mparser.py | 20 | ||||
-rw-r--r-- | test cases/common/190 escape and unicode/meson.build | 1 | ||||
-rw-r--r-- | test cases/failing/72 invalid escape char/meson.build | 4 |
3 files changed, 21 insertions, 4 deletions
diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py index bf7c271..9e43065 100644 --- a/mesonbuild/mparser.py +++ b/mesonbuild/mparser.py @@ -40,8 +40,16 @@ ESCAPE_SEQUENCE_MULTI_RE = re.compile(r''' | \\[\\abfnrtv] # Single-character escapes )''', re.UNICODE | re.VERBOSE) +class MesonUnicodeDecodeError(MesonException): + def __init__(self, match): + super().__init__("%s" % match) + self.match = match + def decode_match(match): - return codecs.decode(match.group(0), 'unicode_escape') + try: + return codecs.decode(match.group(0), 'unicode_escape') + except UnicodeDecodeError as err: + raise MesonUnicodeDecodeError(match.group(0)) class ParseException(MesonException): def __init__(self, text, line, lineno, colno): @@ -172,11 +180,17 @@ class Lexer: mlog.warning("""Newline character in a string detected, use ''' (three single quotes) for multiline strings instead. This will become a hard error in a future Meson release.""", self.getline(line_start), lineno, col) value = match_text[1:-1] - value = ESCAPE_SEQUENCE_SINGLE_RE.sub(decode_match, value) + try: + value = ESCAPE_SEQUENCE_SINGLE_RE.sub(decode_match, value) + except MesonUnicodeDecodeError as err: + raise MesonException("Failed to parse escape sequence: '{}' in string:\n {}".format(err.match, match_text)) elif tid == 'multiline_string': tid = 'string' value = match_text[3:-3] - value = ESCAPE_SEQUENCE_MULTI_RE.sub(decode_match, value) + try: + value = ESCAPE_SEQUENCE_MULTI_RE.sub(decode_match, value) + except MesonUnicodeDecodeError as err: + raise MesonException("Failed to parse escape sequence: '{}' in string:\n{}".format(err.match, match_text)) lines = match_text.split('\n') if len(lines) > 1: lineno += len(lines) - 1 diff --git a/test cases/common/190 escape and unicode/meson.build b/test cases/common/190 escape and unicode/meson.build index be11073..65377b6 100644 --- a/test cases/common/190 escape and unicode/meson.build +++ b/test cases/common/190 escape and unicode/meson.build @@ -22,4 +22,3 @@ foreach l : find_file_list.stdout().strip('\x00').split('\x00') endforeach test('second', executable('second', found_files_hex + [gen_file])) - diff --git a/test cases/failing/72 invalid escape char/meson.build b/test cases/failing/72 invalid escape char/meson.build new file mode 100644 index 0000000..b4e9196 --- /dev/null +++ b/test cases/failing/72 invalid escape char/meson.build @@ -0,0 +1,4 @@ +# Make sure meson exits on invalid string +# The string below contains an invalid unicode code point + +'my name is what \uxyzo who are you' |