Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Format characters ``%s`` and ``%V`` in :c:func:`PyUnicode_FromFormat` and
``%s`` in :c:func:`PyBytes_FromFormat` no longer read memory past the
limit if *precision* is specified.
12 changes: 9 additions & 3 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,15 @@ PyBytes_FromFormatV(const char *format, va_list vargs)
Py_ssize_t i;

p = va_arg(vargs, const char*);
i = strlen(p);
if (prec > 0 && i > prec)
i = prec;
if (prec <= 0) {
i = strlen(p);
}
else {
i = 0;
while (i < prec && p[i]) {
i++;
}
}
s = _PyBytesWriter_WriteBytes(&writer, s, p, i);
if (s == NULL)
goto error;
Expand Down
12 changes: 9 additions & 3 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2579,9 +2579,15 @@ unicode_fromformat_write_cstr(_PyUnicodeWriter *writer, const char *str,
PyObject *unicode;
int res;

length = strlen(str);
if (precision != -1)
length = Py_MIN(length, precision);
if (precision == -1) {
length = strlen(str);
}
else {
length = 0;
while (length < precision && str[length]) {
length++;
}
}
unicode = PyUnicode_DecodeUTF8Stateful(str, length, "replace", NULL);
if (unicode == NULL)
return -1;
Expand Down