]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107450: Raise OverflowError when parser column offset overflows (#110754)
authorLysandros Nikolaou <lisandrosnik@gmail.com>
Thu, 12 Oct 2023 09:34:12 +0000 (11:34 +0200)
committerGitHub <noreply@github.com>
Thu, 12 Oct 2023 09:34:12 +0000 (09:34 +0000)
Lib/test/test_exceptions.py
Parser/pegen_errors.c

index 05a89e7705e90f42d071b6ef2545ab446012c92b..bba2eeb8877730a5a24f1effcc75f4d17b01454c 100644 (file)
@@ -318,6 +318,10 @@ class ExceptionTests(unittest.TestCase):
         check('(yield i) = 2', 1, 2)
         check('def f(*):\n  pass', 1, 7)
 
+    def testMemoryErrorBigSource(self):
+        with self.assertRaisesRegex(OverflowError, "column offset overflow"):
+            exec(f"if True:\n {' ' * 2**31}print('hello world')")
+
     @cpython_only
     def testSettingException(self):
         # test that setting an exception at the C level works even if the
index 2baae5289b5667ce6ec381a7b3fdf442997f8e6e..15e99e23d8490f21d01ce72048c5cccf722e3fe2 100644 (file)
@@ -235,6 +235,12 @@ _PyPegen_raise_error(Parser *p, PyObject *errtype, int use_mark, const char *err
             col_offset = 0;
         } else {
             const char* start = p->tok->buf  ? p->tok->line_start : p->tok->buf;
+            if (p->tok->cur - start > INT_MAX) {
+                PyErr_SetString(PyExc_OverflowError,
+                    "Parser column offset overflow - source line is too big");
+                p->error_indicator = 1;
+                return NULL;
+            }
             col_offset = Py_SAFE_DOWNCAST(p->tok->cur - start, intptr_t, int);
         }
     } else {