gh-96670: Raise SyntaxError when parsing NULL bytes (#97594)

This commit is contained in:
Pablo Galindo Salgado 2022-09-27 23:23:42 +01:00 committed by GitHub
parent dd53b79de0
commit aab01e3524
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 65 additions and 21 deletions

View file

@ -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();
}