Skip to content

[3.15] gh-158445: Detect invalid UCS4 characters in debug mode (#158502) - #159524

Merged
vstinner merged 2 commits into
python:3.15from
vstinner:invalid_str15
Oct 16, 2026
Merged

vstinner merged 2 commits into
python:3.15from
vstinner:invalid_str15

Conversation

@vstinner

@vstinner vstinner commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

…#158502)

In debug mode, PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND) and
PyUnicodeWriter_WriteUCS4() now raise a SystemError if a character is
not in range [U+0000; U+10ffff], instead of creating an invalid str
object.

In release mode, PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND)
raises a SystemError if the size is 1.

* In debug mode, unicode_char() no longer fails with an assertion
  error if the character is invalid. Instead, raise SystemError.
* Add tests on invalid UCS-4 characters.
* Add _testinternalcapi._Py_MAX_UNICODE.
* Add unicode_invalid_character() helper function.

(cherry picked from commit a14a4a6)
…ertions (python#158604)

If Python is built in release mode with assertions,
PyUnicode_FromKindAndData() and PyUnicodeWriter_Finish() fail with an
assertion error on invalid characters.

(cherry picked from commit 35bdbca)
@read-the-docs-community

Copy link
Copy Markdown

@vstinner

Copy link
Copy Markdown
Member Author

The 3.14 backport will rely on #159525 (not merged yet).

@vstinner

Copy link
Copy Markdown
Member Author

"x86_64-apple-darwin/clang (Debug)" and "x86_64-apple-darwin/clang (Release)" failed on installing LLVM, make was interrupted after 60 minutes:

==> Installing dependencies for llvm@21: cmake, readline, sqlite, xz, pkgconf and python@3.15
==> Upgrading llvm@21 dependency: cmake (4.4.2 -> 4.4.4)
==> ./bootstrap --prefix=/usr/local/Cellar/cmake/4.4.4 --no-system-libs --parall
==> make
Error: The operation was canceled.

@vstinner
vstinner merged commit 2c23505 into python:3.15 Oct 16, 2026
94 of 98 checks passed
@vstinner
vstinner deleted the invalid_str15 branch October 16, 2026 14:47
@miss-islington-app

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @vstinner, I could not cleanly backport this to 3.14 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker 2c235059117a9b12d1650876d8413e80a734c31a 3.14

@bedevere-app

bedevere-app Bot commented Oct 16, 2026

Copy link
Copy Markdown

GH-159540 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Oct 16, 2026
vstinner added a commit that referenced this pull request Oct 16, 2026
…) (#159524) (#159540)

[3.15] gh-158445: Detect invalid UCS4 characters in debug mode (#158502) (#159524)

* gh-158445: Detect invalid UCS4 characters in debug mode (#158502)

In debug mode, PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND) and
PyUnicodeWriter_WriteUCS4() now raise a SystemError if a character is
not in range [U+0000; U+10ffff], instead of creating an invalid str
object.

In release mode, PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND)
raises a SystemError if the size is 1.

* In debug mode, unicode_char() no longer fails with an assertion
  error if the character is invalid. Instead, raise SystemError.
* Add tests on invalid UCS-4 characters.
* Add unicode_invalid_character() helper function.

(cherry picked from commit a14a4a6)

* gh-158445: Skip invalid UCS-4 tests if Python is built with assertions (#158604)

If Python is built in release mode with assertions,
PyUnicode_FromKindAndData() and PyUnicodeWriter_Finish() fail with an
assertion error on invalid characters.

(cherry picked from commit 35bdbca)
(cherry picked from commit 2c23505)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant