summaryrefslogtreecommitdiffstats
path: root/Parser/tokenizer.c
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2022-11-20 22:30:15 (GMT)
committerGitHub <noreply@github.com>2022-11-20 22:30:15 (GMT)
commitad47c7d926a9f842a31247f4a15b5bb9f1566749 (patch)
tree236bd290258b5a7f65201737daf4713edb95c7e4 /Parser/tokenizer.c
parent88b101ff52010f795b34e3afc04c0e934d662d82 (diff)
downloadcpython-ad47c7d926a9f842a31247f4a15b5bb9f1566749.zip
cpython-ad47c7d926a9f842a31247f4a15b5bb9f1566749.tar.gz
cpython-ad47c7d926a9f842a31247f4a15b5bb9f1566749.tar.bz2
[3.10] gh-99581: Fix a buffer overflow in the tokenizer when copying lines that fill the available buffer (GH-99605). (#99630)
Diffstat (limited to 'Parser/tokenizer.c')
-rw-r--r--Parser/tokenizer.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index 0bbf1b1..13b666c 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -419,7 +419,11 @@ tok_readline_recode(struct tok_state *tok) {
error_ret(tok);
goto error;
}
- if (!tok_reserve_buf(tok, buflen + 1)) {
+ // Make room for the null terminator *and* potentially
+ // an extra newline character that we may need to artificially
+ // add.
+ size_t buffer_size = buflen + 2;
+ if (!tok_reserve_buf(tok, buffer_size)) {
goto error;
}
memcpy(tok->inp, buf, buflen);
@@ -973,6 +977,7 @@ tok_underflow_file(struct tok_state *tok) {
return 0;
}
if (tok->inp[-1] != '\n') {
+ assert(tok->inp + 1 < tok->end);
/* Last line does not end in \n, fake one */
*tok->inp++ = '\n';
*tok->inp = '\0';