8000 bpo-44396: Update multi-line-start location when reallocating tokeniz… · python/cpython@e766306 · GitHub
[go: up one dir, main page]

Skip to content

Commit e766306

Browse files
committed
bpo-44396: Update multi-line-start location when reallocating tokenizer buffers
1 parent e7b4644 commit e766306

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/test/test_eof.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ def test_EOFS(self):
2929
else:
3030
raise support.TestFailed
3131

32+
def test_EOFS_with_file(self):
33+
expect = ("(<string>, line 1)")
34+
with os_helper.temp_dir() as temp_dir:
35+
file_name = script_helper.make_script(temp_dir, 'foo', """'''this is \na \ntest""")
36+
rc, out, err = script_helper.assert_python_failure(file_name)
37+
self.assertIn(b'unterminated triple-quoted string literal (detected at line 3)', err)
38+
3239
def test_eof_with_line_continuation(self):
3340
expect = "unexpected EOF while parsing (<string>, line 1)"
3441
try:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a possible crash in the tokenizer when raising syntax errors for
2+
unclosed strings. Patch by Pablo Galindo.

Parser/tokenizer.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ tok_reserve_buf(struct tok_state *tok, Py_ssize_t size)
372372
if (newsize > tok->end - tok->buf) {
373373
char *newbuf = tok->buf;
374374
Py_ssize_t start = tok->start == NULL ? -1 : tok->start - tok->buf;
375+
Py_ssize_t line_start = tok->start == NULL ? -1 : tok->line_start - tok->buf;
376+
Py_ssize_t multi_line_start = tok->multi_line_start - tok->buf;
375377
newbuf = (char *)PyMem_Realloc(newbuf, newsize);
376378
if (newbuf == NULL) {
377379
tok->done = E_NOMEM;
@@ -382,6 +384,8 @@ tok_reserve_buf(struct tok_state *tok, Py_ssize_t size)
382384
tok->inp = tok->buf + oldsize;
383385
tok->end = tok->buf + newsize;
384386
tok->start = start < 0 ? NULL : tok->buf + start;
387+
tok->line_start = line_start < 0 ? NULL : tok->buf + line_start;
388+
tok->multi_line_start = multi_line_start < 0 ? NULL : tok->buf + multi_line_start;
385389
}
386390
return 1;
387391
}
@@ -1883,6 +1887,7 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
18831887
while (end_quote_size != quote_size) {
18841888
c = tok_nextc(tok);
18851889
if (c == EOF || (quote_size == 1 && c == '\n')) {
1890+
assert(tok->multi_line_start != NULL);
18861891
// shift the tok_state's location into
18871892
// the start of string, and report the error
18881893
// from the initial quote character

0 commit comments

Comments
 (0)
0