Skip to content

Commit ea3ac56

Browse files
[3.12] gh-107450: Raise OverflowError when parser column offset overflows (GH-110754) (#110762)
(cherry picked from commit fb7843e) Co-authored-by: Lysandros Nikolaou <[email protected]>
1 parent e8d0419 commit ea3ac56

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

Lib/test/test_exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ def baz():
318318
check('(yield i) = 2', 1, 2)
319319
check('def f(*):\n pass', 1, 7)
320320

321+
def testMemoryErrorBigSource(self):
322+
with self.assertRaisesRegex(OverflowError, "column offset overflow"):
323+
exec(f"if True:\n {' ' * 2**31}print('hello world')")
324+
321325
@cpython_only
322326
def testSettingException(self):
323327
# test that setting an exception at the C level works even if the

Parser/pegen_errors.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ _PyPegen_raise_error(Parser *p, PyObject *errtype, int use_mark, const char *err
233233
col_offset = 0;
234234
} else {
235235
const char* start = p->tok->buf ? p->tok->line_start : p->tok->buf;
236+
if (p->tok->cur - start > INT_MAX) {
237+
PyErr_SetString(PyExc_OverflowError,
238+
"Parser column offset overflow - source line is too big");
239+
p->error_indicator = 1;
240+
return NULL;
241+
}
236242
col_offset = Py_SAFE_DOWNCAST(p->tok->cur - start, intptr_t, int);
237243
}
238244
} else {

0 commit comments

Comments
 (0)