]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-146196: Fix Undefined Behavior in _PyUnicodeWriter_WriteASCIIString() (#146201)
authorShamil <ashm.tech@proton.me>
Fri, 20 Mar 2026 15:58:41 +0000 (18:58 +0300)
committerGitHub <noreply@github.com>
Fri, 20 Mar 2026 15:58:41 +0000 (15:58 +0000)
Avoid calling memcpy(data + writer->pos, NULL, 0)
which has an undefined behavior.

Co-authored-by: Victor Stinner <vstinner@python.org>
Misc/NEWS.d/next/Core_and_Builtins/2026-03-20-13-55-14.gh-issue-146196.Zg70Kb.rst [new file with mode: 0644]
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 (file)
index 0000000..9e03c1b
--- /dev/null
@@ -0,0 +1,2 @@
+Fix potential Undefined Behavior in :c:func:`PyUnicodeWriter_WriteASCII` by
+adding a zero-length check. Patch by Shamil Abdulaev.
index cd2688e32dfaf8d096b4d678f2f238c1a9495181..a753c9b971c702c74d73493fb5ed1ff8d9fc6909 100644 (file)
@@ -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) {