]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46339: Fix crash in the parser when computing error text for multi-line f-strings...
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Tue, 11 Jan 2022 16:30:39 +0000 (16:30 +0000)
committerGitHub <noreply@github.com>
Tue, 11 Jan 2022 16:30:39 +0000 (08:30 -0800)
Automerge-Triggered-By: GH:pablogsal
Lib/test/test_exceptions.py
Misc/NEWS.d/next/Core and Builtins/2022-01-11-11-50-19.bpo-46339.OVumDZ.rst [new file with mode: 0644]
Parser/pegen_errors.c

index e4d685f4154ed84addf4025efc35cf4a008c7aea..531b9c92deae5adf4aae0e20cb7a76ddf6b6c8c0 100644 (file)
@@ -280,6 +280,12 @@ class ExceptionTests(unittest.TestCase):
             }
             \"\"\"
             }'''""", 5, 17)
+        check('''f"""
+
+
+            {
+            6
+            0="""''', 5, 13)
 
         # Errors thrown by symtable.c
         check('x = [(yield i) for i in range(3)]', 1, 7)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-01-11-11-50-19.bpo-46339.OVumDZ.rst b/Misc/NEWS.d/next/Core and Builtins/2022-01-11-11-50-19.bpo-46339.OVumDZ.rst
new file mode 100644 (file)
index 0000000..cd04f06
--- /dev/null
@@ -0,0 +1,3 @@
+Fix a crash in the parser when retrieving the error text for multi-line
+f-strings expressions that do not start in the first line of the string.
+Patch by Pablo Galindo
index f07d9d8a34df7024dd6607377e4181eaf3b3b650..bffae8532ca2b7e9efa3f7729f897e89e13bef08 100644 (file)
@@ -250,8 +250,15 @@ get_error_line_from_tokenizer_buffers(Parser *p, Py_ssize_t lineno)
     char *cur_line = p->tok->fp_interactive ? p->tok->interactive_src_start : p->tok->str;
     assert(cur_line != NULL);
 
-    for (int i = 0; i < lineno - 1; i++) {
-        cur_line = strchr(cur_line, '\n') + 1;
+    Py_ssize_t relative_lineno = p->starting_lineno ? lineno - p->starting_lineno + 1 : lineno;
+
+    for (int i = 0; i < relative_lineno - 1; i++) {
+        char *new_line = strchr(cur_line, '\n') + 1;
+        assert(new_line != NULL && new_line < p->tok->inp);
+        if (new_line == NULL || new_line >= p->tok->inp) {
+            break;
+        }
+        cur_line = new_line;
     }
 
     char *next_newline;