diff options
author | Eric V. Smith <ericvsmith@users.noreply.github.com> | 2022-01-25 02:53:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-25 02:53:27 (GMT) |
commit | 0daf72194bd4e31de7f12020685bb39a14d6f45e (patch) | |
tree | 829de3225fde1183fd0ad337af070361daa316e6 /Parser/string_parser.c | |
parent | 7c26472d09548905d8c158b26b6a2b12de6cdc32 (diff) | |
download | cpython-0daf72194bd4e31de7f12020685bb39a14d6f45e.zip cpython-0daf72194bd4e31de7f12020685bb39a14d6f45e.tar.gz cpython-0daf72194bd4e31de7f12020685bb39a14d6f45e.tar.bz2 |
bpo-46503: Prevent an assert from firing when parsing some invalid \N sequences in f-strings. (GH-30865)
* bpo-46503: Prevent an assert from firing. Also fix one nearby tiny PEP-7 nit.
* Added blurb.
Diffstat (limited to 'Parser/string_parser.c')
-rw-r--r-- | Parser/string_parser.c | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/Parser/string_parser.c b/Parser/string_parser.c index 57d9b9e..0b5e30b 100644 --- a/Parser/string_parser.c +++ b/Parser/string_parser.c @@ -442,12 +442,23 @@ fstring_find_literal(Parser *p, const char **str, const char *end, int raw, if (!raw && ch == '\\' && s < end) { ch = *s++; if (ch == 'N') { + /* We need to look at and skip matching braces for "\N{name}" + sequences because otherwise we'll think the opening '{' + starts an expression, which is not the case with "\N". + Keep looking for either a matched '{' '}' pair, or the end + of the string. */ + if (s < end && *s++ == '{') { while (s < end && *s++ != '}') { } continue; } - break; + + /* This is an invalid "\N" sequence, since it's a "\N" not + followed by a "{". Just keep parsing this literal. This + error will be caught later by + decode_unicode_with_escapes(). */ + continue; } if (ch == '{' && warn_invalid_escape_sequence(p, ch, t) < 0) { return -1; @@ -491,7 +502,8 @@ done: *literal = PyUnicode_DecodeUTF8Stateful(literal_start, s - literal_start, NULL, NULL); - } else { + } + else { *literal = decode_unicode_with_escapes(p, literal_start, s - literal_start, t); } |