summaryrefslogtreecommitdiffstats
path: root/Parser/pegen_errors.c
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2022-03-26 16:29:02 (GMT)
committerGitHub <noreply@github.com>2022-03-26 16:29:02 (GMT)
commit26cca8067bf5306e372c0e90036d832c5021fd90 (patch)
treed348b658e593bcae572d6b119de9e239bbbf1cc4 /Parser/pegen_errors.c
parentee912ad6f66bb8cf5a8a2b4a7ecd2752bf070864 (diff)
downloadcpython-26cca8067bf5306e372c0e90036d832c5021fd90.zip
cpython-26cca8067bf5306e372c0e90036d832c5021fd90.tar.gz
cpython-26cca8067bf5306e372c0e90036d832c5021fd90.tar.bz2
bpo-47117: Don't crash if we fail to decode characters when the tokenizer buffers are uninitialized (GH-32129)
Automerge-Triggered-By: GH:pablogsal
Diffstat (limited to 'Parser/pegen_errors.c')
-rw-r--r--Parser/pegen_errors.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/Parser/pegen_errors.c b/Parser/pegen_errors.c
index 0be9df0..4896996 100644
--- a/Parser/pegen_errors.c
+++ b/Parser/pegen_errors.c
@@ -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
+ // 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;
@@ -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) {