Skip to content

Commit c9214b9

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

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

Lib/test/test_exceptions.py

+4
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

+6
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ _PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...)
224224
col_offset = 0;
225225
} else {
226226
const char* start = p->tok->buf ? p->tok->line_start : p->tok->buf;
227+
if (p->tok->cur - start > INT_MAX) {
228+
PyErr_SetString(PyExc_OverflowError,
229+
"Parser column offset overflow - source line is too big");
230+
p->error_indicator = 1;
231+
return NULL;
232+
}
227233
col_offset = Py_SAFE_DOWNCAST(p->tok->cur - start, intptr_t, int);
228234
}
229235
} else {

0 commit comments

Comments
 (0)