Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-47117: Don't crash if we fail to decode characters when the tokenizer buffers are uninitialized #32129

Merged
merged 2 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash if we fail to decode characters in interactive mode if the
tokenizer buffers are uninitialized. Patch by Pablo Galindo.
9 changes: 7 additions & 2 deletions Parser/pegen_errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,12 @@ get_error_line_from_tokenizer_buffers(Parser *p, Py_ssize_t lineno)
assert((p->tok->fp == NULL && p->tok->str != NULL) || p->tok->fp == stdin);

char *cur_line = p->tok->fp_interactive ? p->tok->interactive_src_start : p->tok->str;
assert(cur_line != NULL);
if (cur_line == NULL) {
assert(p->tok->fp_interactive);
// We can reach this point if the tokenizer buffers for interactive source have not been
pablogsal marked this conversation as resolved.
Show resolved Hide resolved
// initialized because we failed to decode the original source with the given locale.
return PyUnicode_FromStringAndSize("", 0);
}

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

if (p->tok->fp_interactive) {
if (p->tok->fp_interactive && p->tok->interactive_src_start != NULL) {
error_line = get_error_line_from_tokenizer_buffers(p, lineno);
}
else if (p->start_rule == Py_file_input) {
Expand Down