Skip to content

Commit 555755e

Browse files
[2.7] bpo-35552: Fix reading past the end in PyString_FromFormat(). (GH-11276) (GH-11534)
Format character "%s" in PyString_FromFormat() no longer read memory past the limit if precision is specified. (cherry picked from commit d586ccb)
1 parent 08a81df commit 555755e

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Format character ``%s`` in :c:func:`PyString_FromFormat` no longer read
2+
memory past the limit if *precision* is specified.

Objects/stringobject.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,15 @@ PyString_FromFormatV(const char *format, va_list vargs)
360360
break;
361361
case 's':
362362
p = va_arg(vargs, char*);
363-
i = strlen(p);
364-
if (n > 0 && i > n)
365-
i = n;
363+
if (n <= 0) {
364+
i = strlen(p);
365+
}
366+
else {
367+
i = 0;
368+
while (i < n && p[i]) {
369+
i++;
370+
}
371+
}
366372
Py_MEMCPY(s, p, i);
367373
s += i;
368374
break;

0 commit comments

Comments
 (0)