Skip to content

Commit cedec19

Browse files
authored
bpo-46339: Fix crash in the parser when computing error text for multi-line f-strings (GH-30529)
Automerge-Triggered-By: GH:pablogsal
1 parent 43c5c13 commit cedec19

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Lib/test/test_exceptions.py

+6
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ def baz():
280280
}
281281
\"\"\"
282282
}'''""", 5, 17)
283+
check('''f"""
284+
285+
286+
{
287+
6
288+
0="""''', 5, 13)
283289

284290
# Errors thrown by symtable.c
285291
check('x = [(yield i) for i in range(3)]', 1, 7)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash in the parser when retrieving the error text for multi-line
2+
f-strings expressions that do not start in the first line of the string.
3+
Patch by Pablo Galindo

Parser/pegen_errors.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,15 @@ get_error_line_from_tokenizer_buffers(Parser *p, Py_ssize_t lineno)
250250
char *cur_line = p->tok->fp_interactive ? p->tok->interactive_src_start : p->tok->str;
251251
assert(cur_line != NULL);
252252

253-
for (int i = 0; i < lineno - 1; i++) {
254-
cur_line = strchr(cur_line, '\n') + 1;
253+
Py_ssize_t relative_lineno = p->starting_lineno ? lineno - p->starting_lineno + 1 : lineno;
254+
255+
for (int i = 0; i < relative_lineno - 1; i++) {
256+
char *new_line = strchr(cur_line, '\n') + 1;
257+
assert(new_line != NULL && new_line < p->tok->inp);
258+
if (new_line == NULL || new_line >= p->tok->inp) {
259+
break;
260+
}
261+
cur_line = new_line;
255262
}
256263

257264
char *next_newline;

0 commit comments

Comments
 (0)