Skip to content

Commit 1af7b7d

Browse files
lysnikolaouFFY00serhiy-storchaka
authored
[3.11] gh-107450: Check for overflow in the tokenizer and fix overflow test (GH-110832) (#110939)
(cherry picked from commit a1ac559) Co-authored-by: Filipe Laíns <[email protected]> Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 73ebe2f commit 1af7b7d

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

Include/errcode.h

+20-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
extern "C" {
55
#endif
66

7-
87
/* Error codes passed around between file input, tokenizer, parser and
98
interpreter. This is necessary so we can turn them into Python
109
exceptions at a higher level. Note that some errors have a
@@ -13,24 +12,26 @@ extern "C" {
1312
the parser only returns E_EOF when it hits EOF immediately, and it
1413
never returns E_OK. */
1514

16-
#define E_OK 10 /* No error */
17-
#define E_EOF 11 /* End Of File */
18-
#define E_INTR 12 /* Interrupted */
19-
#define E_TOKEN 13 /* Bad token */
20-
#define E_SYNTAX 14 /* Syntax error */
21-
#define E_NOMEM 15 /* Ran out of memory */
22-
#define E_DONE 16 /* Parsing complete */
23-
#define E_ERROR 17 /* Execution error */
24-
#define E_TABSPACE 18 /* Inconsistent mixing of tabs and spaces */
25-
#define E_OVERFLOW 19 /* Node had too many children */
26-
#define E_TOODEEP 20 /* Too many indentation levels */
27-
#define E_DEDENT 21 /* No matching outer block for dedent */
28-
#define E_DECODE 22 /* Error in decoding into Unicode */
29-
#define E_EOFS 23 /* EOF in triple-quoted string */
30-
#define E_EOLS 24 /* EOL in single-quoted string */
31-
#define E_LINECONT 25 /* Unexpected characters after a line continuation */
32-
#define E_BADSINGLE 27 /* Ill-formed single statement input */
33-
#define E_INTERACT_STOP 28 /* Interactive mode stopped tokenization */
15+
#define E_OK 10 /* No error */
16+
#define E_EOF 11 /* End Of File */
17+
#define E_INTR 12 /* Interrupted */
18+
#define E_TOKEN 13 /* Bad token */
19+
#define E_SYNTAX 14 /* Syntax error */
20+
#define E_NOMEM 15 /* Ran out of memory */
21+
#define E_DONE 16 /* Parsing complete */
22+
#define E_ERROR 17 /* Execution error */
23+
#define E_TABSPACE 18 /* Inconsistent mixing of tabs and spaces */
24+
#define E_OVERFLOW 19 /* Node had too many children */
25+
#define E_TOODEEP 20 /* Too many indentation levels */
26+
#define E_DEDENT 21 /* No matching outer block for dedent */
27+
#define E_DECODE 22 /* Error in decoding into Unicode */
28+
#define E_EOFS 23 /* EOF in triple-quoted string */
29+
#define E_EOLS 24 /* EOL in single-quoted string */
30+
#define E_LINECONT 25 /* Unexpected characters after a line continuation */
31+
#define E_BADSINGLE 27 /* Ill-formed single statement input */
32+
#define E_INTERACT_STOP 28 /* Interactive mode stopped tokenization */
33+
#define E_COLUMNOVERFLOW 29 /* Column offset overflow */
34+
3435

3536
#ifdef __cplusplus
3637
}

Lib/test/test_exceptions.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
from test.support.warnings_helper import check_warnings
2020
from test import support
2121

22+
try:
23+
from _testcapi import INT_MAX
24+
except ImportError:
25+
INT_MAX = 2**31 - 1
26+
27+
2228

2329
class NaiveException(Exception):
2430
def __init__(self, x):
@@ -318,11 +324,13 @@ def baz():
318324
check('(yield i) = 2', 1, 2)
319325
check('def f(*):\n pass', 1, 7)
320326

327+
@unittest.skipIf(INT_MAX >= sys.maxsize, "Downcasting to int is safe for col_offset")
321328
@support.requires_resource('cpu')
322-
@support.bigmemtest(support._2G, memuse=1.5)
323-
def testMemoryErrorBigSource(self, _size):
324-
with self.assertRaises(OverflowError):
325-
exec(f"if True:\n {' ' * 2**31}print('hello world')")
329+
@support.bigmemtest(INT_MAX, memuse=2, dry_run=False)
330+
def testMemoryErrorBigSource(self, size):
331+
src = b"if True:\n%*s" % (size, b"pass")
332+
with self.assertRaisesRegex(OverflowError, "Parser column offset overflow"):
333+
compile(src, '<fragment>', 'exec')
326334

327335
@cpython_only
328336
def testSettingException(self):

Parser/pegen_errors.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ _Pypegen_tokenizer_error(Parser *p)
101101
msg = "unexpected character after line continuation character";
102102
break;
103103
}
104+
case E_COLUMNOVERFLOW:
105+
PyErr_SetString(PyExc_OverflowError,
106+
"Parser column offset overflow - source line is too big");
107+
return -1;
104108
default:
105109
msg = "unknown parsing error";
106110
}
@@ -224,12 +228,6 @@ _PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...)
224228
col_offset = 0;
225229
} else {
226230
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-
}
233231
col_offset = Py_SAFE_DOWNCAST(p->tok->cur - start, intptr_t, int);
234232
}
235233
} else {

Parser/tokenizer.c

+4
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,10 @@ tok_nextc(struct tok_state *tok)
10571057
int rc;
10581058
for (;;) {
10591059
if (tok->cur != tok->inp) {
1060+
if (tok->cur - tok->buf >= INT_MAX) {
1061+
tok->done = E_COLUMNOVERFLOW;
1062+
return EOF;
1063+
}
10601064
return Py_CHARMASK(*tok->cur++); /* Fast path */
10611065
}
10621066
if (tok->done != E_OK) {

0 commit comments

Comments
 (0)