Skip to content

Commit cd10a2e

Browse files
ashm-devvstinner
andauthored
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 <vstinner@python.org>
1 parent 82a24a4 commit cd10a2e

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix potential Undefined Behavior in :c:func:`PyUnicodeWriter_WriteASCII` by
2+
adding a zero-length check. Patch by Shamil Abdulaev.

Objects/unicode_writer.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,10 @@ _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer,
465465
if (len == -1)
466466
len = strlen(ascii);
467467

468+
if (len == 0) {
469+
return 0;
470+
}
471+
468472
assert(ucs1lib_find_max_char((const Py_UCS1*)ascii, (const Py_UCS1*)ascii + len) < 128);
469473

470474
if (writer->buffer == NULL && !writer->overallocate) {

0 commit comments

Comments
 (0)