Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-96268: Fix loading invalid UTF-8#96270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
11 commits Select commit Hold shift + click to select a range
e4aaa14 gh-96268: Fix loading invalid UTF-8
mdboom 407eef7 Add blurb
mdboom e453819 Merge remote-tracking branch 'upstream/main' into fix-valid-utf8
mdboom 3d60ff7 Fix blurb
mdboom 5cc57ad Use assertIn instead
mdboom ad4de7a Fix reference to other decoding function
mdboom 18927b1 Fix coding style
mdboom f741a9d Add comments about handled code ranges in each branch
mdboom f8e9e6e Fix line number in error message
mdboom ace4a8c Remove obsolete comment
mdboom df074a8 PEP7
mdboom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Core and Builtins/2022-08-25-10-19-34.gh-issue-96268.AbYrLB.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Loading a file with invalid UTF-8 will now report the broken character at | ||
| the correct location. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -489,25 +489,59 @@ static void fp_ungetc(int c, struct tok_state *tok){ | ||
| /* Check whether the characters at s start a valid | ||
| UTF-8 sequence. Return the number of characters forming | ||
| the sequence if yes, 0 if not. */ | ||
| static int valid_utf8(const unsigned char* s) | ||
| the sequence if yes, 0 if not. The special cases match | ||
| those in stringlib/codecs.h:utf8_decode. | ||
| */ | ||
| static int | ||
| valid_utf8(const unsigned char* s) | ||
| { | ||
| int expected = 0; | ||
| int length; | ||
| if (*s < 0x80) | ||
| if (*s < 0x80){ | ||
| /* single-byte code */ | ||
| return 1; | ||
| if (*s < 0xc0) | ||
| /* following byte */ | ||
| return 0; | ||
| if (*s < 0xE0) | ||
| } | ||
| else if (*s < 0xE0){ | ||
| /* \xC2\x80-\xDF\xBF -- 0080-07FF */ | ||
| if (*s < 0xC2){ | ||
| /* invalid sequence | ||
| \x80-\xBF -- continuation byte | ||
| \xC0-\xC1 -- fake 0000-007F */ | ||
| return 0; | ||
| } | ||
| expected = 1; | ||
| else if (*s < 0xF0) | ||
| } | ||
| else if (*s < 0xF0){ | ||
| /* \xE0\xA0\x80-\xEF\xBF\xBF -- 0800-FFFF */ | ||
| if (*s == 0xE0 && *(s + 1) < 0xA0){ | ||
| /* invalid sequence | ||
| \xE0\x80\x80-\xE0\x9F\xBF -- fake 0000-0800 */ | ||
| return 0; | ||
| } | ||
| else if (*s == 0xED && *(s + 1) >= 0xA0){ | ||
| /* Decoding UTF-8 sequences in range \xED\xA0\x80-\xED\xBF\xBF | ||
| will result in surrogates in range D800-DFFF. Surrogates are | ||
| not valid UTF-8 so they are rejected. | ||
| See https://www.unicode.org/versions/Unicode5.2.0/ch03.pdf | ||
| (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ | ||
| return 0; | ||
| } | ||
| expected = 2; | ||
| else if (*s < 0xF8) | ||
| } | ||
| else if (*s < 0xF5){ | ||
| /* \xF0\x90\x80\x80-\xF4\x8F\xBF\xBF -- 10000-10FFFF */ | ||
| if (*(s + 1) < 0x90 ? *s == 0xF0 : *s == 0xF4){ | ||
| /* invalid sequence -- one of: | ||
| \xF0\x80\x80\x80-\xF0\x8F\xBF\xBF -- fake 0000-FFFF | ||
| \xF4\x90\x80\x80- -- 110000- overflow */ | ||
| return 0; | ||
| } | ||
gvanrossum marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| expected = 3; | ||
| else | ||
| } | ||
| else{ | ||
| /* invalid start byte */ | ||
| return 0; | ||
| } | ||
| length = expected + 1; | ||
| for (; expected; expected--) | ||
| if (s[expected] < 0x80 || s[expected] >= 0xC0) | ||
| @@ -528,14 +562,12 @@ ensure_utf8(char *line, struct tok_state *tok) | ||
| } | ||
| } | ||
| if (badchar){ | ||
| /* Need to add 1 to the line number, since this line | ||
| has not been counted, yet. */ | ||
| PyErr_Format(PyExc_SyntaxError, | ||
| "Non-UTF-8 code starting with '\\x%.2x' " | ||
| "in file %U on line %i, " | ||
| "but no encoding declared; " | ||
| "see https://peps.python.org/pep-0263/ for details", | ||
| badchar, tok->filename, tok->lineno + 1); | ||
| badchar, tok->filename, tok->lineno); | ||
| return 0; | ||
| } | ||
| return 1; | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.