summaryrefslogtreecommitdiff
path: root/Parser
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2022-09-27 23:23:42 +0100
committerGitHub <noreply@github.com>2022-09-27 23:23:42 +0100
commitaab01e3524d966dca6e72c718a2c71403a14e47c (patch)
tree69e30e5354e49bc104177ab61ee8b39ccd24bf22 /Parser
parentdd53b79de0ea98af6a11481217a961daef4e9774 (diff)
downloadcpython-git-aab01e3524d966dca6e72c718a2c71403a14e47c.tar.gz
gh-96670: Raise SyntaxError when parsing NULL bytes (#97594)
Diffstat (limited to 'Parser')
-rw-r--r--Parser/tokenizer.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index b3b11855f4..3c37fd9c45 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -378,6 +378,11 @@ tok_reserve_buf(struct tok_state *tok, Py_ssize_t size)
return 1;
}
+static inline int
+contains_null_bytes(const char* str, size_t size) {
+ return memchr(str, 0, size) != NULL;
+}
+
static int
tok_readline_recode(struct tok_state *tok) {
PyObject *line;
@@ -829,9 +834,9 @@ tok_readline_raw(struct tok_state *tok)
if (!tok_reserve_buf(tok, BUFSIZ)) {
return 0;
}
- char *line = Py_UniversalNewlineFgets(tok->inp,
- (int)(tok->end - tok->inp),
- tok->fp, NULL);
+ int n_chars = (int)(tok->end - tok->inp);
+ size_t line_size = 0;
+ char *line = _Py_UniversalNewlineFgetsWithSize(tok->inp, n_chars, tok->fp, NULL, &line_size);
if (line == NULL) {
return 1;
}
@@ -839,7 +844,7 @@ tok_readline_raw(struct tok_state *tok)
tok_concatenate_interactive_new_line(tok, line) == -1) {
return 0;
}
- tok->inp = strchr(tok->inp, '\0');
+ tok->inp += line_size;
if (tok->inp == tok->buf) {
return 0;
}
@@ -1075,6 +1080,12 @@ tok_nextc(struct tok_state *tok)
return EOF;
}
tok->line_start = tok->cur;
+
+ if (contains_null_bytes(tok->line_start, tok->inp - tok->line_start)) {
+ syntaxerror(tok, "source code cannot contain null bytes");
+ tok->cur = tok->inp;
+ return EOF;
+ }
}
Py_UNREACHABLE();
}