@@ -198,99 +198,99 @@ Py_UCS4 _PyUnicode_ToLowercase(Py_UCS4 ch)
198198 return ch + ctype -> lower ;
199199}
200200
201- int PyUnicode_ToLower (Py_UCS4 ch , Py_UCS4 * res , int size )
201+ Py_ssize_t PyUnicode_ToLower (Py_UCS4 ch , Py_UCS4 * res , Py_ssize_t size )
202202{
203203 const _PyUnicode_TypeRecord * ctype = gettyperecord (ch );
204204
205205 if (ctype -> flags & EXTENDED_CASE_MASK ) {
206206 int index = ctype -> lower & 0xFFFF ;
207207 int n = ctype -> lower >> 24 ;
208+ if (n > size ) {
209+ PyErr_SetString (PyExc_ValueError , "output buffer is too small" );
210+ return -1 ;
211+ }
212+
208213 int i ;
209- for (i = 0 ; i < n ; i ++ ) {
210- if (i >= size ) {
211- PyErr_SetString (PyExc_ValueError , "output buffer is too small" );
212- return -1 ;
213- }
214+ for (i = 0 ; i < n ; i ++ )
214215 res [i ] = _PyUnicode_ExtendedCase [index + i ];
215- }
216216 return n ;
217217 }
218218
219- if (0 >= size ) {
219+ if (size < 1 ) {
220220 PyErr_SetString (PyExc_ValueError , "output buffer is too small" );
221221 return -1 ;
222222 }
223223 res [0 ] = ch + ctype -> lower ;
224224 return 1 ;
225225}
226226
227- int PyUnicode_ToTitle (Py_UCS4 ch , Py_UCS4 * res , int size )
227+ Py_ssize_t PyUnicode_ToTitle (Py_UCS4 ch , Py_UCS4 * res , Py_ssize_t size )
228228{
229229 const _PyUnicode_TypeRecord * ctype = gettyperecord (ch );
230230
231231 if (ctype -> flags & EXTENDED_CASE_MASK ) {
232232 int index = ctype -> title & 0xFFFF ;
233233 int n = ctype -> title >> 24 ;
234+ if (n > size ) {
235+ PyErr_SetString (PyExc_ValueError , "output buffer is too small" );
236+ return -1 ;
237+ }
238+
234239 int i ;
235- for (i = 0 ; i < n ; i ++ ) {
236- if (i >= size ) {
237- PyErr_SetString (PyExc_ValueError , "output buffer is too small" );
238- return -1 ;
239- }
240+ for (i = 0 ; i < n ; i ++ )
240241 res [i ] = _PyUnicode_ExtendedCase [index + i ];
241- }
242242 return n ;
243243 }
244244
245- if (0 >= size ) {
245+ if (size < 1 ) {
246246 PyErr_SetString (PyExc_ValueError , "output buffer is too small" );
247247 return -1 ;
248248 }
249249 res [0 ] = ch + ctype -> title ;
250250 return 1 ;
251251}
252252
253- int PyUnicode_ToUpper (Py_UCS4 ch , Py_UCS4 * res , int size )
253+ Py_ssize_t PyUnicode_ToUpper (Py_UCS4 ch , Py_UCS4 * res , Py_ssize_t size )
254254{
255255 const _PyUnicode_TypeRecord * ctype = gettyperecord (ch );
256256
257257 if (ctype -> flags & EXTENDED_CASE_MASK ) {
258258 int index = ctype -> upper & 0xFFFF ;
259259 int n = ctype -> upper >> 24 ;
260+ if (n > size ) {
261+ PyErr_SetString (PyExc_ValueError , "output buffer is too small" );
262+ return -1 ;
263+ }
264+
260265 int i ;
261- for (i = 0 ; i < n ; i ++ ) {
262- if (i >= size ) {
263- PyErr_SetString (PyExc_ValueError , "output buffer is too small" );
264- return -1 ;
265- }
266+ for (i = 0 ; i < n ; i ++ )
266267 res [i ] = _PyUnicode_ExtendedCase [index + i ];
267- }
268268 return n ;
269269 }
270270
271- if (0 >= size ) {
271+ if (size < 1 ) {
272272 PyErr_SetString (PyExc_ValueError , "output buffer is too small" );
273273 return -1 ;
274274 }
275275 res [0 ] = ch + ctype -> upper ;
276276 return 1 ;
277277}
278278
279- int PyUnicode_ToFolded (Py_UCS4 ch , Py_UCS4 * res , int size )
279+ Py_ssize_t PyUnicode_ToFolded (Py_UCS4 ch , Py_UCS4 * res , Py_ssize_t size )
280280{
281281 const _PyUnicode_TypeRecord * ctype = gettyperecord (ch );
282282
283283 if (ctype -> flags & EXTENDED_CASE_MASK && (ctype -> lower >> 20 ) & 7 ) {
284284 int index = (ctype -> lower & 0xFFFF ) + (ctype -> lower >> 24 );
285285 int n = (ctype -> lower >> 20 ) & 7 ;
286+ if (n > size ) {
287+ PyErr_SetString (PyExc_ValueError , "output buffer is too small" );
288+ return -1 ;
289+ }
290+
286291 int i ;
287- for (i = 0 ; i < n ; i ++ ) {
288- if (i >= size ) {
289- PyErr_SetString (PyExc_ValueError , "output buffer is too small" );
290- return -1 ;
291- }
292+ for (i = 0 ; i < n ; i ++ )
292293 res [i ] = _PyUnicode_ExtendedCase [index + i ];
293- }
294294 return n ;
295295 }
296296
0 commit comments