@@ -561,13 +561,14 @@ fill_number(PyObject *out, Py_ssize_t pos, const NumberFieldWidths *spec,
561561 return -1 ;
562562 if (toupper ) {
563563 Py_ssize_t t ;
564- /* XXX if the upper-case prefix is wider than the target
565- buffer, the caller should have allocated a wider string,
566- but currently doesn't. */
567- for (t = 0 ; t < spec -> n_prefix ; ++ t )
568- PyUnicode_WRITE (kind , data , pos + t ,
569- Py_UNICODE_TOUPPER (
570- PyUnicode_READ (kind , data , pos + t )));
564+ for (t = 0 ; t < spec -> n_prefix ; t ++ ) {
565+ Py_UCS4 c = PyUnicode_READ (kind , data , pos + t );
566+ if (c > 127 ) {
567+ PyErr_SetString (PyExc_SystemError , "prefix not ASCII" );
568+ return -1 ;
569+ }
570+ PyUnicode_WRITE (kind , data , pos + t , Py_TOUPPER (c ));
571+ }
571572 }
572573 pos += spec -> n_prefix ;
573574 }
@@ -607,10 +608,14 @@ fill_number(PyObject *out, Py_ssize_t pos, const NumberFieldWidths *spec,
607608 }
608609 if (toupper ) {
609610 Py_ssize_t t ;
610- for (t = 0 ; t < spec -> n_grouped_digits ; ++ t )
611- PyUnicode_WRITE (kind , data , pos + t ,
612- Py_UNICODE_TOUPPER (
613- PyUnicode_READ (kind , data , pos + t )));
611+ for (t = 0 ; t < spec -> n_grouped_digits ; t ++ ) {
612+ Py_UCS4 c = PyUnicode_READ (kind , data , pos + t );
613+ if (c > 127 ) {
614+ PyErr_SetString (PyExc_SystemError , "non-ascii grouped digit" );
615+ return -1 ;
616+ }
617+ PyUnicode_WRITE (kind , data , pos + t , Py_TOUPPER (c ));
618+ }
614619 }
615620 pos += spec -> n_grouped_digits ;
616621
0 commit comments