File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
733739class ByteArrayTest (BaseBytesTest , unittest .TestCase ):
734740 type2test = bytearray
Original file line number Diff line number Diff line change @@ -10,6 +10,9 @@ What's New in Python 3.3.4 release candidate 1?
1010Core 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
Original file line number Diff line number Diff 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 ));
You can’t perform that action at this time.
0 commit comments