(Merge 3.3) Issue #19969: PyBytes_FromFormatV() now raises an OverflowError if

"%c" argument is not in range [0; 255].
This commit is contained in:
Victor Stinner 2013-12-13 12:15:31 +01:00
commit 507ac3a591
3 changed files with 25 additions and 3 deletions

View File

@ -743,6 +743,12 @@ class BytesTest(BaseBytesTest, unittest.TestCase):
self.assertEqual(PyBytes_FromFormat(b's:%s', c_char_p(b'cstr')), self.assertEqual(PyBytes_FromFormat(b's:%s', c_char_p(b'cstr')),
b's:cstr') b's:cstr')
# Issue #19969
self.assertRaises(OverflowError,
PyBytes_FromFormat, b'%c', c_int(-1))
self.assertRaises(OverflowError,
PyBytes_FromFormat, b'%c', c_int(256))
class ByteArrayTest(BaseBytesTest, unittest.TestCase): class ByteArrayTest(BaseBytesTest, unittest.TestCase):
type2test = bytearray type2test = bytearray

View File

@ -10,6 +10,9 @@ Release date: 2014-01-05
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #19969: PyBytes_FromFormatV() now raises an OverflowError if "%c"
argument is not in range [0; 255].
- Issue #19787: PyThread_set_key_value() now always set the value. In Python - Issue #19787: PyThread_set_key_value() now always set the value. In Python
3.3, the function did nothing if the key already exists (if the current value 3.3, the function did nothing if the key already exists (if the current value
is a non-NULL pointer). is a non-NULL pointer).

View File

@ -195,8 +195,17 @@ PyBytes_FromFormatV(const char *format, va_list vargs)
switch (*f) { switch (*f) {
case 'c': case 'c':
(void)va_arg(count, int); {
/* fall through... */ int c = va_arg(count, int);
if (c < 0 || c > 255) {
PyErr_SetString(PyExc_OverflowError,
"PyBytes_FromFormatV(): %c format "
"expects an integer in range [0; 255]");
return NULL;
}
n++;
break;
}
case '%': case '%':
n++; n++;
break; break;
@ -276,8 +285,12 @@ PyBytes_FromFormatV(const char *format, va_list vargs)
switch (*f) { switch (*f) {
case 'c': case 'c':
*s++ = va_arg(vargs, int); {
int c = va_arg(vargs, int);
/* c has been checked for overflow in the first step */
*s++ = (unsigned char)c;
break; break;
}
case 'd': case 'd':
if (longflag) if (longflag)
sprintf(s, "%ld", va_arg(vargs, long)); sprintf(s, "%ld", va_arg(vargs, long));