Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -875,10 +875,16 @@ xmlcharrefreplace(PyBytesWriter *writer, char *str,

/* generate replacement */
for (i = collstart; i < collend; ++i) {
size = sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i));
if (size < 0) {
return NULL;
}
// Use snprintf() with a temporary buffer to not write the trailing
// NUL byte in the writer buffer.
Py_BUILD_ASSERT(_Py_MAX_UNICODE <= 0x10ffff);
// len('&#1114111;\0') is 11 bytes.
char buffer[11];
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
size = snprintf(buffer, sizeof(buffer), "&#%d;", ch);
assert(4 <= size && (size_t)size <= (sizeof(buffer) - 1));

memcpy(str, buffer, size);
str += size;
}
return str;
Expand Down
Loading