https://github.com/python/cpython/commit/cd10a2e65c25682095f6ee4a9b9a181938a50d2e commit: cd10a2e65c25682095f6ee4a9b9a181938a50d2e branch: main author: Shamil <[email protected]> committer: vstinner <[email protected]> date: 2026-03-20T15:58:41Z summary:
gh-146196: Fix Undefined Behavior in _PyUnicodeWriter_WriteASCIIString() (#146201) Avoid calling memcpy(data + writer->pos, NULL, 0) which has an undefined behavior. Co-authored-by: Victor Stinner <[email protected]> files: A Misc/NEWS.d/next/Core_and_Builtins/2026-03-20-13-55-14.gh-issue-146196.Zg70Kb.rst M Objects/unicode_writer.c diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-20-13-55-14.gh-issue-146196.Zg70Kb.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-20-13-55-14.gh-issue-146196.Zg70Kb.rst new file mode 100644 index 00000000000000..9e03c1bbb0e1cb --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-20-13-55-14.gh-issue-146196.Zg70Kb.rst @@ -0,0 +1,2 @@ +Fix potential Undefined Behavior in :c:func:`PyUnicodeWriter_WriteASCII` by +adding a zero-length check. Patch by Shamil Abdulaev. diff --git a/Objects/unicode_writer.c b/Objects/unicode_writer.c index cd2688e32dfaf8..a753c9b971c702 100644 --- a/Objects/unicode_writer.c +++ b/Objects/unicode_writer.c @@ -465,6 +465,10 @@ _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer, if (len == -1) len = strlen(ascii); + if (len == 0) { + return 0; + } + assert(ucs1lib_find_max_char((const Py_UCS1*)ascii, (const Py_UCS1*)ascii + len) < 128); if (writer->buffer == NULL && !writer->overallocate) { _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
