]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-107450: Raise OverflowError when parser column offset overflows (GH-110754...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 12 Oct 2023 09:57:36 +0000 (11:57 +0200)
committerGitHub <noreply@github.com>
Thu, 12 Oct 2023 09:57:36 +0000 (09:57 +0000)
(cherry picked from commit fb7843ee895ac7f6eeb58f356b1a320eea081cfc)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Lib/test/test_exceptions.py
Parser/pegen_errors.c

index 6f34e29e42f7c7416b50ebbe17d1a18663854d1b..7f6eaa34e11406251fbe4a8903b76528112ee15b 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 3d8cccb0a9749c6f54016b46d67f0f5765f2f8c0..ea5c4e227ff7637e519d3aa08a7db52c07aba5e0 100644 (file)
@@ -224,6 +224,12 @@ _PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...)
             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 {