summaryrefslogtreecommitdiffstats
path: root/Parser
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2021-03-14 03:38:40 (GMT)
committerGitHub <noreply@github.com>2021-03-14 03:38:40 (GMT)
commitcd8dcbc851fcc312722cdb5544c2f25cf46b3f8a (patch)
tree6a16b4054f682f075245ca5bd917995bbb18c096 /Parser
parent9923df96413a0b480a34ec1d537b66ca0eeb0fdc (diff)
downloadcpython-cd8dcbc851fcc312722cdb5544c2f25cf46b3f8a.zip
cpython-cd8dcbc851fcc312722cdb5544c2f25cf46b3f8a.tar.gz
cpython-cd8dcbc851fcc312722cdb5544c2f25cf46b3f8a.tar.bz2
bpo-43410: Fix crash in the parser when producing syntax errors when reading from stdin (GH-24763)
Diffstat (limited to 'Parser')
-rw-r--r--Parser/pegen.c14
-rw-r--r--Parser/tokenizer.c78
-rw-r--r--Parser/tokenizer.h4
3 files changed, 66 insertions, 30 deletions
diff --git a/Parser/pegen.c b/Parser/pegen.c
index 68f0e32..3011993 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -397,7 +397,8 @@ get_error_line(Parser *p, Py_ssize_t lineno)
are stored in p->tok->stdin_content */
assert(p->tok->fp == NULL || p->tok->fp == stdin);
- char *cur_line = p->tok->fp == NULL ? p->tok->str : p->tok->stdin_content;
+ char *cur_line = p->tok->fp_interactive ? p->tok->interactive_src_start : p->tok->str;
+
for (int i = 0; i < lineno - 1; i++) {
cur_line = strchr(cur_line, '\n') + 1;
}
@@ -440,7 +441,10 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
goto error;
}
- if (p->start_rule == Py_file_input) {
+ if (p->tok->fp_interactive) {
+ error_line = get_error_line(p, lineno);
+ }
+ else if (p->start_rule == Py_file_input) {
error_line = PyErr_ProgramTextObject(p->tok->filename, (int) lineno);
}
@@ -1232,7 +1236,7 @@ _PyPegen_run_parser(Parser *p)
if (p->fill == 0) {
RAISE_SYNTAX_ERROR("error at start before reading any input");
}
- else if (p->tok->done == E_EOF) {
+ else if (p->tok->done == E_EOF) {
if (p->tok->level) {
raise_unclosed_parentheses_error(p);
} else {
@@ -1287,6 +1291,10 @@ _PyPegen_run_parser_from_file_pointer(FILE *fp, int start_rule, PyObject *filena
}
return NULL;
}
+ if (!tok->fp || ps1 != NULL || ps2 != NULL ||
+ PyUnicode_CompareWithASCIIString(filename_ob, "<stdin>") == 0) {
+ tok->fp_interactive = 1;
+ }
// This transfers the ownership to the tokenizer
tok->filename = filename_ob;
Py_INCREF(filename_ob);
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index d9334aa..09d8b88 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -56,6 +56,9 @@ tok_new(void)
if (tok == NULL)
return NULL;
tok->buf = tok->cur = tok->inp = NULL;
+ tok->fp_interactive = 0;
+ tok->interactive_src_start = NULL;
+ tok->interactive_src_end = NULL;
tok->start = NULL;
tok->end = NULL;
tok->done = E_OK;
@@ -80,8 +83,6 @@ tok_new(void)
tok->decoding_readline = NULL;
tok->decoding_buffer = NULL;
tok->type_comments = 0;
- tok->stdin_content = NULL;
-
tok->async_hacks = 0;
tok->async_def = 0;
tok->async_def_indent = 0;
@@ -323,6 +324,35 @@ check_bom(int get_char(struct tok_state *),
return 1;
}
+static int tok_concatenate_interactive_new_line(struct tok_state* tok, char* line) {
+ assert(tok->fp_interactive);
+
+ if (!line) {
+ return 0;
+ }
+
+ Py_ssize_t current_size = tok->interactive_src_end - tok->interactive_src_start;
+ Py_ssize_t line_size = strlen(line);
+ char* new_str = tok->interactive_src_start;
+
+ new_str = PyMem_Realloc(new_str, current_size + line_size + 1);
+ if (!new_str) {
+ if (tok->interactive_src_start) {
+ PyMem_Free(tok->interactive_src_start);
+ }
+ tok->interactive_src_start = NULL;
+ tok->interactive_src_end = NULL;
+ tok->done = E_NOMEM;
+ return -1;
+ }
+ strcpy(new_str + current_size, line);
+
+ tok->interactive_src_start = new_str;
+ tok->interactive_src_end = new_str + current_size + line_size;
+ return 0;
+}
+
+
/* Read a line of text from TOK into S, using the stream in TOK.
Return NULL on failure, else S.
@@ -552,6 +582,12 @@ decoding_fgets(char *s, int size, struct tok_state *tok)
badchar, tok->filename, tok->lineno + 1);
return error_ret(tok);
}
+
+ if (tok->fp_interactive &&
+ tok_concatenate_interactive_new_line(tok, line) == -1) {
+ return NULL;
+ }
+
return line;
}
@@ -807,17 +843,21 @@ PyTokenizer_FromFile(FILE *fp, const char* enc,
void
PyTokenizer_Free(struct tok_state *tok)
{
- if (tok->encoding != NULL)
+ if (tok->encoding != NULL) {
PyMem_Free(tok->encoding);
+ }
Py_XDECREF(tok->decoding_readline);
Py_XDECREF(tok->decoding_buffer);
Py_XDECREF(tok->filename);
- if (tok->fp != NULL && tok->buf != NULL)
+ if (tok->fp != NULL && tok->buf != NULL) {
PyMem_Free(tok->buf);
- if (tok->input)
+ }
+ if (tok->input) {
PyMem_Free(tok->input);
- if (tok->stdin_content)
- PyMem_Free(tok->stdin_content);
+ }
+ if (tok->interactive_src_start != NULL) {
+ PyMem_Free(tok->interactive_src_start);
+ }
PyMem_Free(tok);
}
@@ -858,24 +898,6 @@ tok_nextc(struct tok_state *tok)
if (translated == NULL)
return EOF;
newtok = translated;
- if (tok->stdin_content == NULL) {
- tok->stdin_content = PyMem_Malloc(strlen(translated) + 1);
- if (tok->stdin_content == NULL) {
- tok->done = E_NOMEM;
- return EOF;
- }
- sprintf(tok->stdin_content, "%s", translated);
- }
- else {
- char *new_str = PyMem_Malloc(strlen(tok->stdin_content) + strlen(translated) + 1);
- if (new_str == NULL) {
- tok->done = E_NOMEM;
- return EOF;
- }
- sprintf(new_str, "%s%s", tok->stdin_content, translated);
- PyMem_Free(tok->stdin_content);
- tok->stdin_content = new_str;
- }
}
if (tok->encoding && newtok && *newtok) {
/* Recode to UTF-8 */
@@ -898,6 +920,10 @@ tok_nextc(struct tok_state *tok)
strcpy(newtok, buf);
Py_DECREF(u);
}
+ if (tok->fp_interactive &&
+ tok_concatenate_interactive_new_line(tok, newtok) == -1) {
+ return EOF;
+ }
if (tok->nextprompt != NULL)
tok->prompt = tok->nextprompt;
if (newtok == NULL)
@@ -958,7 +984,7 @@ tok_nextc(struct tok_state *tok)
}
if (decoding_fgets(tok->buf, (int)(tok->end - tok->buf),
tok) == NULL) {
- if (!tok->decoding_erred)
+ if (!tok->decoding_erred && !(tok->done == E_NOMEM))
tok->done = E_EOF;
done = 1;
}
diff --git a/Parser/tokenizer.h b/Parser/tokenizer.h
index 56074b6..111126c 100644
--- a/Parser/tokenizer.h
+++ b/Parser/tokenizer.h
@@ -26,6 +26,9 @@ struct tok_state {
char *buf; /* Input buffer, or NULL; malloc'ed if fp != NULL */
char *cur; /* Next character in buffer */
char *inp; /* End of data in buffer */
+ int fp_interactive; /* If the file descriptor is interactive */
+ char *interactive_src_start; /* The start of the source parsed so far in interactive mode */
+ char *interactive_src_end; /* The end of the source parsed so far in interactive mode */
const char *end; /* End of input buffer if buf != NULL */
const char *start; /* Start of current token if not NULL */
int done; /* E_OK normally, E_EOF at EOF, otherwise error code */
@@ -37,7 +40,6 @@ struct tok_state {
int atbol; /* Nonzero if at begin of new line */
int pendin; /* Pending indents (if > 0) or dedents (if < 0) */
const char *prompt, *nextprompt; /* For interactive prompting */
- char *stdin_content;
int lineno; /* Current line number */
int first_lineno; /* First line of a single line or multi line string
expression (cf. issue 16806) */