Skip to content

Commit ce7e8c4

Browse files
committed
bpo-47117: Don't crash if we fail to decode characters when the tokenizer buffers are uninitialized
1 parent ee912ad commit ce7e8c4

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,11 @@ 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+
// We can reach this point if the tokenizer buffers for interactive source have not been
253+
// initialized because we failed to decode the original source with the given locale.
254+
return PyUnicode_FromStringAndSize("", 0);
255+
}
252256

253257
Py_ssize_t relative_lineno = p->starting_lineno ? lineno - p->starting_lineno + 1 : lineno;
254258
const char* buf_end = p->tok->fp_interactive ? p->tok->interactive_src_end : p->tok->inp;
@@ -311,7 +315,7 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
311315
goto error;
312316
}
313317

314-
if (p->tok->fp_interactive) {
318+
if (p->tok->fp_interactive && p->tok->interactive_src_start != NULL) {
315319
error_line = get_error_line_from_tokenizer_buffers(p, lineno);
316320
}
317321
else if (p->start_rule == Py_file_input) {

0 commit comments

Comments
 (0)