Skip to content

Commit 26cca80

Browse files
authored
bpo-47117: Don't crash if we fail to decode characters when the tokenizer buffers are uninitialized (GH-32129)
Automerge-Triggered-By: GH:pablogsal
1 parent ee912ad commit 26cca80

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash if we fail to decode characters in interactive mode if the
2+
tokenizer buffers are uninitialized. Patch by Pablo Galindo.

Parser/pegen_errors.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,12 @@ get_error_line_from_tokenizer_buffers(Parser *p, Py_ssize_t lineno)
248248
assert((p->tok->fp == NULL && p->tok->str != NULL) || p->tok->fp == stdin);
249249

250250
char *cur_line = p->tok->fp_interactive ? p->tok->interactive_src_start : p->tok->str;
251-
assert(cur_line != NULL);
251+
if (cur_line == NULL) {
252+
assert(p->tok->fp_interactive);
253+
// We can reach this point if the tokenizer buffers for interactive source have not been
254+
// initialized because we failed to decode the original source with the given locale.
255+
return PyUnicode_FromStringAndSize("", 0);
256+
}
252257

253258
Py_ssize_t relative_lineno = p->starting_lineno ? lineno - p->starting_lineno + 1 : lineno;
254259
const char* buf_end = p->tok->fp_interactive ? p->tok->interactive_src_end : p->tok->inp;
@@ -311,7 +316,7 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
311316
goto error;
312317
}
313318

314-
if (p->tok->fp_interactive) {
319+
if (p->tok->fp_interactive && p->tok->interactive_src_start != NULL) {
315320
error_line = get_error_line_from_tokenizer_buffers(p, lineno);
316321
}
317322
else if (p->start_rule == Py_file_input) {

0 commit comments

Comments
 (0)