Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9362cf

Browse files
committed
Issue #19969: PyBytes_FromFormatV() now raises an OverflowError if "%c"
argument is not in range [0; 255].
1 parent 3ad2d70 commit c9362cf

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,12 @@ def test_from_format(self):
729729
self.assertEqual(PyBytes_FromFormat(b's:%s', c_char_p(b'cstr')),
730730
b's:cstr')
731731

732+
# Issue #19969
733+
self.assertRaises(OverflowError,
734+
PyBytes_FromFormat, b'%c', c_int(-1))
735+
self.assertRaises(OverflowError,
736+
PyBytes_FromFormat, b'%c', c_int(256))
737+
732738

733739
class ByteArrayTest(BaseBytesTest, unittest.TestCase):
734740
type2test = bytearray

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3.4 release candidate 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #19969: PyBytes_FromFormatV() now raises an OverflowError if "%c"
14+
argument is not in range [0; 255].
15+
1316
- Issue #14432: Generator now clears the borrowed reference to the thread
1417
state. Fix a crash when a generator is created in a C thread that is
1518
destroyed while the generator is still used. The issue was that a generator

Objects/bytesobject.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,17 @@ PyBytes_FromFormatV(const char *format, va_list vargs)
186186

187187
switch (*f) {
188188
case 'c':
189-
(void)va_arg(count, int);
190-
/* fall through... */
189+
{
190+
int c = va_arg(count, int);
191+
if (c < 0 || c > 255) {
192+
PyErr_SetString(PyExc_OverflowError,
193+
"PyBytes_FromFormatV(): %c format "
194+
"expects an integer in range [0; 255]");
195+
return NULL;
196+
}
197+
n++;
198+
break;
199+
}
191200
case '%':
192201
n++;
193202
break;
@@ -267,8 +276,12 @@ PyBytes_FromFormatV(const char *format, va_list vargs)
267276

268277
switch (*f) {
269278
case 'c':
270-
*s++ = va_arg(vargs, int);
279+
{
280+
int c = va_arg(vargs, int);
281+
/* c has been checked for overflow in the first step */
282+
*s++ = (unsigned char)c;
271283
break;
284+
}
272285
case 'd':
273286
if (longflag)
274287
sprintf(s, "%ld", va_arg(vargs, long));

0 commit comments

Comments
 (0)