Repository navigation
Extended ASCII characters in multiline strings cause "SystemError: Negative size passed to PyUnicode_New" when the encoding is not specified #96611
Copy link
Copy link
Closed
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-unicodetype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Sep 6, 2022 #96270 may fix this. Let me confirm.
#96270 may fix this. Let me confirm.
It does not fix this.
Since copy-and-paste doesn't usually preserve broken encodings, this is a convenient way to reproduce the bug:
open("x.py", "wb").write(b'print("""\n\xb1""")')$ python x.pyIn
tok_get()in "Parser/tokenizer.c", the following code blindly handlesEOFreturned fromtok_nextc()as if it's the end of the file.Lines 1936 to 1948 in 6744490
/* Get rest of string */ while (end_quote_size != quote_size) { c = tok_nextc(tok); if (c == EOF || (quote_size == 1 && c == '\n')) { assert(tok->multi_line_start != NULL); // shift the tok_state's location into // the start of string, and report the error // from the initial quote character tok->cur = (char *)tok->start; tok->cur++; tok->line_start = tok->multi_line_start; int start = tok->lineno; tok->lineno = tok->first_lineno; In this case, however,
tok->doneisE_DECODEinstead ofE_EOF. This gets set byerror_ret(), which also clearstok->startandtok->curtoNULL. The above code incrementstok->curto 1. Subsequently,_syntaxerror_range()tries to decode the text for the syntax error using the negative size1 - tok->line_start.- addedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on Sep 6, 2022 - added a commit that references this issue
on Sep 6, 2022 - added a commit that references this issue
on Sep 6, 2022 - added 4 commits that reference this issue
on Sep 6, 2022
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-unicodetype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Bug report
In some cases, when dealing with multi-line string with non-utf8 encoded files, python will throw a
SystemError: Negative size passed to PyUnicode_Newand not execute any code.Minimal test case:
This is only a problem if the non-utf8 character lies on a new line (at any point in the line)
A similar test case behaves correctly
And reports an encoding warning, which is the expected behavior
Since this is an encoding related errors, both files are attached (as .txt, GitHub does not allow .py attachments).
test.txt - single line (correct behavior)
test_ml.txt - multi line (bug)
My environment