diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-10-12 09:57:36 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-12 09:57:36 (GMT) |
commit | c9214b90f469aa85e307a6dc3b6052c961003807 (patch) | |
tree | 91debcb32d087ec6d56b83258767fcfc89b6a772 | |
parent | 5178fb0b89586be2b572bae7fc04a7e63168eee8 (diff) | |
download | cpython-c9214b90f469aa85e307a6dc3b6052c961003807.zip cpython-c9214b90f469aa85e307a6dc3b6052c961003807.tar.gz cpython-c9214b90f469aa85e307a6dc3b6052c961003807.tar.bz2 |
[3.11] gh-107450: Raise OverflowError when parser column offset overflows (GH-110754) (#110763)
(cherry picked from commit fb7843ee895ac7f6eeb58f356b1a320eea081cfc)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
-rw-r--r-- | Lib/test/test_exceptions.py | 4 | ||||
-rw-r--r-- | Parser/pegen_errors.c | 6 |
2 files changed, 10 insertions, 0 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 6f34e29..7f6eaa3 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -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 diff --git a/Parser/pegen_errors.c b/Parser/pegen_errors.c index 3d8cccb..ea5c4e2 100644 --- a/Parser/pegen_errors.c +++ b/Parser/pegen_errors.c @@ -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 { |