bpo-35552: Fix reading past the end in PyUnicode_FromFormat() and PyBytes_FromFormat(). (GH-11276)

Format characters "%s" and "%V" in PyUnicode_FromFormat() and "%s" in PyBytes_FromFormat()
no longer read memory past the limit if precision is specified.
This commit is contained in:
Serhiy Storchaka 2019-01-12 10:30:35 +02:00 • committed by GitHub
parent f1ec3cefad
commit d586ccb04f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 6 deletions

View file

@ -312,9 +312,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;