-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-70278: Fix PyUnicode_FromFormat() with precision for %s and %V #120365
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
serhiy-storchaka
merged 6 commits into
python:main
from
serhiy-storchaka:PyUnicode_FromFormat-truncate
Jun 24, 2024
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e969269
gh-70278: Fix PyUnicode_FromFormat() with precision for %s and %V
serhiy-storchaka 6f80f9c
Fix a case when the precision is larger than strlen().
serhiy-storchaka fdc6d4d
Merge branch 'main' into PyUnicode_FromFormat-truncate
serhiy-storchaka 4000cdf
Add a comment and refactor.
serhiy-storchaka c628994
Merge branch 'main' into PyUnicode_FromFormat-truncate
serhiy-storchaka 928202a
Merge remote-tracking branch 'refs/remotes/origin/PyUnicode_FromForma…
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/C API/2024-06-11-21-38-32.gh-issue-70278.WDE4zM.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,4 @@ | ||
:c:func:`PyUnicode_FromFormat` no longer produces the ending ``\ufffd`` | ||
character for truncated C string when use precision with ``%s`` and ``%V``. | ||
It now truncates the string before the start of truncated multibyte | ||
sequences. | ||
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 |
---|---|---|
|
@@ -2390,23 +2390,27 @@ unicode_fromformat_write_utf8(_PyUnicodeWriter *writer, const char *str, | |
{ | ||
/* UTF-8 */ | ||
Py_ssize_t length; | ||
Py_ssize_t consumed; | ||
Py_ssize_t *pconsumed; | ||
if (precision == -1) { | ||
length = strlen(str); | ||
pconsumed = NULL; | ||
} | ||
else { | ||
length = 0; | ||
while (length < precision && str[length]) { | ||
length++; | ||
} | ||
pconsumed = (length < precision) ? NULL : &consumed; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment explaining why you set pconsumed? Explain the expected behavior of truncating incomplete sequence at the end, but still replace invalid sequence in the middle. Something like that :-) You can add a reference to the issue gh-70278. |
||
} | ||
|
||
if (width < 0) { | ||
return unicode_decode_utf8_writer(writer, str, length, | ||
_Py_ERROR_REPLACE, "replace", NULL); | ||
_Py_ERROR_REPLACE, "replace", pconsumed); | ||
} | ||
|
||
PyObject *unicode = PyUnicode_DecodeUTF8Stateful(str, length, | ||
"replace", NULL); | ||
"replace", pconsumed); | ||
if (unicode == NULL) | ||
return -1; | ||
|
||
|
Oops, something went wrong.
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.