Skip to content

Commit

Permalink
pythongh-107450: Raise OverflowError when parser column offset overfl…
Browse files Browse the repository at this point in the history
…ows (python#110754)
  • Loading branch information
lysnikolaou authored Oct 12, 2023
1 parent 3d18034 commit fb7843e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ def baz():
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
Expand Down
6 changes: 6 additions & 0 deletions Parser/pegen_errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,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 {
Expand Down

0 comments on commit fb7843e

Please sign in to comment.