Skip to content

Commit ca11535

Browse files
ashm-devvstinner
andcommitted
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> (cherry picked from commit cd10a2e)
1 parent 2105187 commit ca11535

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/unicodeobject.c

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

14057+
if (len == 0) {
14058+
return 0;
14059+
}
14060+
1405714061
assert(ucs1lib_find_max_char((const Py_UCS1*)ascii, (const Py_UCS1*)ascii + len) < 128);
1405814062

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

0 commit comments

Comments
 (0)