From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Thu, 12 Oct 2023 10:03:09 +0000 (+0200) Subject: [3.12] gh-107450: Raise OverflowError when parser column offset overflows (GH-110754... X-Git-Tag: v3.12.1~307 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ea3ac56a05d00da82ffd4b9326654aab4893cb72;p=thirdparty%2FPython%2Fcpython.git [3.12] gh-107450: Raise OverflowError when parser column offset overflows (GH-110754) (#110762) (cherry picked from commit fb7843ee895ac7f6eeb58f356b1a320eea081cfc) Co-authored-by: Lysandros Nikolaou --- diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 72afb3b0fb03..5d460982ddfa 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 cbeb9da04d92..71c476517375 100644 --- a/Parser/pegen_errors.c +++ b/Parser/pegen_errors.c @@ -233,6 +233,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 {