From d241c2a5926c0c8147a05b10ad3e7a54d480f55b Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 23 Jul 2015 23:20:37 +0100 Subject: [PATCH] py/lexer: Raise SyntaxError when str hex escape sequence is malformed. Addresses issue #1390. --- py/lexer.c | 4 ++-- tests/basics/lexer.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/py/lexer.c b/py/lexer.c index 3e7d589f61..17d711696b 100644 --- a/py/lexer.c +++ b/py/lexer.c @@ -454,8 +454,8 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, bool first_token) { { mp_uint_t num = 0; if (!get_hex(lex, (c == 'x' ? 2 : c == 'u' ? 4 : 8), &num)) { - // TODO error message - assert(0); + // not enough hex chars for escape sequence + lex->tok_kind = MP_TOKEN_INVALID; } c = num; break; diff --git a/tests/basics/lexer.py b/tests/basics/lexer.py index 1057c3961b..70a9cce993 100644 --- a/tests/basics/lexer.py +++ b/tests/basics/lexer.py @@ -38,3 +38,21 @@ def a(x): if x: print(x) a(1) + +# badly formed hex escape sequences +try: + exec(r"'\x0'") +except SyntaxError: + print("SyntaxError") +try: + exec(r"b'\x0'") +except SyntaxError: + print("SyntaxError") +try: + exec(r"'\u000'") +except SyntaxError: + print("SyntaxError") +try: + exec(r"'\U0000000'") +except SyntaxError: + print("SyntaxError")