@@ -629,28 +629,27 @@ PyBytes_Repr(PyObject *obj, int smartquotes)
629629 newsize = 3 ; /* b'' */
630630 s = (unsigned char * )op -> ob_sval ;
631631 for (i = 0 ; i < length ; i ++ ) {
632+ Py_ssize_t incr = 1 ;
632633 switch (s [i ]) {
633- case '\'' : squotes ++ ; newsize ++ ; break ;
634- case '"' : dquotes ++ ; newsize ++ ; break ;
634+ case '\'' : squotes ++ ; break ;
635+ case '"' : dquotes ++ ; break ;
635636 case '\\' : case '\t' : case '\n' : case '\r' :
636- newsize + = 2 ; break ; /* \C */
637+ incr = 2 ; break ; /* \C */
637638 default :
638639 if (s [i ] < ' ' || s [i ] >= 0x7f )
639- newsize += 4 ; /* \xHH */
640- else
641- newsize ++ ;
640+ incr = 4 ; /* \xHH */
642641 }
642+ if (newsize > PY_SSIZE_T_MAX - incr )
643+ goto overflow ;
644+ newsize += incr ;
643645 }
644646 quote = '\'' ;
645647 if (smartquotes && squotes && !dquotes )
646648 quote = '"' ;
647- if (squotes && quote == '\'' )
649+ if (squotes && quote == '\'' ) {
650+ if (newsize > PY_SSIZE_T_MAX - squotes )
651+ goto overflow ;
648652 newsize += squotes ;
649-
650- if (newsize > (PY_SSIZE_T_MAX - sizeof (PyUnicodeObject ) - 1 )) {
651- PyErr_SetString (PyExc_OverflowError ,
652- "bytes object is too large to make repr" );
653- return NULL ;
654653 }
655654
656655 v = PyUnicode_New (newsize , 127 );
@@ -682,6 +681,11 @@ PyBytes_Repr(PyObject *obj, int smartquotes)
682681 * p ++ = quote ;
683682 assert (_PyUnicode_CheckConsistency (v , 1 ));
684683 return v ;
684+
685+ overflow :
686+ PyErr_SetString (PyExc_OverflowError ,
687+ "bytes object is too large to make repr" );
688+ return NULL ;
685689}
686690
687691static PyObject *
0 commit comments