summaryrefslogtreecommitdiffstats
path: root/Parser/pegen.c
diff options
context:
space:
mode:
authorƁukasz Langa <lukasz@langa.pl>2021-10-19 20:31:18 (GMT)
committerGitHub <noreply@github.com>2021-10-19 20:31:18 (GMT)
commit5c9cab595e56aeb118bff77ece784dbac30b4338 (patch)
tree35ab9e41afc7da8a42c978848823e2d517d0d5de /Parser/pegen.c
parent325b2c223453203b2fa9ce7b9bcebdbef03adf70 (diff)
downloadcpython-5c9cab595e56aeb118bff77ece784dbac30b4338.zip
cpython-5c9cab595e56aeb118bff77ece784dbac30b4338.tar.gz
cpython-5c9cab595e56aeb118bff77ece784dbac30b4338.tar.bz2
[3.10] bpo-45494: Fix parser crash when reporting errors involving invalid continuation characters (GH-28993) (GH-29070)
There are two errors that this commit fixes: * The parser was not correctly computing the offset and the string source for E_LINECONT errors due to the incorrect usage of strtok(). * The parser was not correctly unwinding the call stack when a tokenizer exception happened in rules involving optionals ('?', [...]) as we always make them return valid results by using the comma operator. We need to check first if we don't have an error before continuing.. (cherry picked from commit a106343f632a99c8ebb0136fa140cf189b4a6a57) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Diffstat (limited to 'Parser/pegen.c')
-rw-r--r--Parser/pegen.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/Parser/pegen.c b/Parser/pegen.c
index 1bb975d..66e4b19 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -371,10 +371,18 @@ tokenizer_error(Parser *p)
errtype = PyExc_IndentationError;
msg = "too many levels of indentation";
break;
- case E_LINECONT:
- col_offset = strlen(strtok(p->tok->buf, "\n")) - 1;
+ case E_LINECONT: {
+ char* loc = strrchr(p->tok->buf, '\n');
+ const char* last_char = p->tok->cur - 1;
+ if (loc != NULL && loc != last_char) {
+ col_offset = p->tok->cur - loc - 1;
+ p->tok->buf = loc;
+ } else {
+ col_offset = last_char - p->tok->buf - 1;
+ }
msg = "unexpected character after line continuation character";
break;
+ }
default:
msg = "unknown parsing error";
}