Skip to content

Commit

Permalink
gh-105718: Fix buffer allocation in tokenizer with readline (#105728)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysnikolaou authored Jun 13, 2023
1 parent d0f1afd commit abfbab6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
10 changes: 10 additions & 0 deletions Lib/test/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2239,6 +2239,16 @@ def test_string(self):
FSTRING_START \'f"\' (1, 0) (1, 2)
FSTRING_MIDDLE 'abc\\\\\\ndef' (1, 2) (2, 3)
FSTRING_END '"' (2, 3) (2, 4)
""")

self.check_tokenize('''\
f"{
a}"''', """\
FSTRING_START 'f"' (1, 0) (1, 2)
LBRACE '{' (1, 2) (1, 3)
NAME 'a' (2, 0) (2, 1)
RBRACE '}' (2, 1) (2, 2)
FSTRING_END '"' (2, 2) (2, 3)
""")

self.check_tokenize(r'Rf"abc\
Expand Down
4 changes: 0 additions & 4 deletions Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1106,11 +1106,7 @@ tok_readline_string(struct tok_state* tok) {
tok->inp += buflen;
*tok->inp = '\0';

if (tok->start == NULL) {
tok->buf = tok->cur;
}
tok->line_start = tok->cur;

Py_DECREF(line);
return 1;
error:
Expand Down
2 changes: 1 addition & 1 deletion Parser/tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ typedef struct _tokenizer_mode {
struct tok_state {
/* Input state; buf <= cur <= inp <= end */
/* NB an entire line is held in the buffer */
char *buf; /* Input buffer, or NULL; malloc'ed if fp != NULL */
char *buf; /* Input buffer, or NULL; malloc'ed if fp != NULL or readline != NULL */
char *cur; /* Next character in buffer */
char *inp; /* End of data in buffer */
int fp_interactive; /* If the file descriptor is interactive */
Expand Down

0 comments on commit abfbab6

Please sign in to comment.