summaryrefslogtreecommitdiffstats
path: root/Parser/string_parser.c
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-01-25 03:13:11 (GMT)
committerGitHub <noreply@github.com>2022-01-25 03:13:11 (GMT)
commit894e8c13484822458d53cc77c9265b7a88450a4b (patch)
tree908ff84d817bbb98db7c531a26482765d271c590 /Parser/string_parser.c
parenteaeb99468045b863d2dd3da3e3d1c3c9c78e1254 (diff)
downloadcpython-894e8c13484822458d53cc77c9265b7a88450a4b.zip
cpython-894e8c13484822458d53cc77c9265b7a88450a4b.tar.gz
cpython-894e8c13484822458d53cc77c9265b7a88450a4b.tar.bz2
bpo-46503: Prevent an assert from firing when parsing some invalid \N sequences in f-strings. (GH-30865) (GH-30866)
* bpo-46503: Prevent an assert from firing. Also fix one nearby tiny PEP-7 nit. * Added blurb. (cherry picked from commit 0daf72194bd4e31de7f12020685bb39a14d6f45e) Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com> Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
Diffstat (limited to 'Parser/string_parser.c')
-rw-r--r--Parser/string_parser.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/Parser/string_parser.c b/Parser/string_parser.c
index c83e63f..4c20435 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);
}