@@ -221,7 +221,8 @@ unicode_copycharacters(PyObject *self, PyObject *args)
221
221
}
222
222
223
223
static PyObject *
224
- unicode_case_operation (PyObject * str , Py_ssize_t (* function )(Py_UCS4 , Py_UCS4 * , Py_ssize_t ))
224
+ unicode_case_operation (PyObject * str , Py_ssize_t (* function )(Py_UCS4 , Py_UCS4 * , Py_ssize_t ),
225
+ Py_UCS4 * buf , Py_ssize_t size )
225
226
{
226
227
if (!PyUnicode_Check (str )) {
227
228
PyErr_Format (PyExc_TypeError , "expect str type, got %T" , str );
@@ -235,42 +236,53 @@ unicode_case_operation(PyObject *str, Py_ssize_t (*function)(Py_UCS4, Py_UCS4 *,
235
236
236
237
Py_UCS4 c = PyUnicode_READ_CHAR (str , 0 );
237
238
238
- Py_UCS4 buf [3 ];
239
- Py_ssize_t chars = function (c , buf , Py_ARRAY_LENGTH (buf ));
239
+ Py_ssize_t chars = function (c , buf , size );
240
240
if (chars < 0 ) {
241
241
return NULL ;
242
242
}
243
243
244
244
return PyUnicode_FromKindAndData (PyUnicode_4BYTE_KIND , buf , chars );
245
245
}
246
246
247
- /* Test PyUnicode_ToLower () */
247
+ /* Test PyUCS4_ToLower () */
248
248
static PyObject *
249
249
unicode_tolower (PyObject * self , PyObject * arg )
250
250
{
251
- return unicode_case_operation (arg , PyUnicode_ToLower );
251
+ Py_UCS4 buf [PyUCS4_CASE_CONVERSION_BUFFER_SIZE ];
252
+ return unicode_case_operation (arg , PyUCS4_ToLower , buf , PyUCS4_CASE_CONVERSION_BUFFER_SIZE );
252
253
}
253
254
254
- /* Test PyUnicode_ToUpper() */
255
+
256
+ /* Test PyUCS4_ToUpper() */
255
257
static PyObject *
256
258
unicode_toupper (PyObject * self , PyObject * arg )
257
259
{
258
- return unicode_case_operation (arg , PyUnicode_ToUpper );
260
+ Py_UCS4 buf [PyUCS4_CASE_CONVERSION_BUFFER_SIZE ];
261
+ return unicode_case_operation (arg , PyUCS4_ToUpper , buf , PyUCS4_CASE_CONVERSION_BUFFER_SIZE );
259
262
}
260
263
264
+ /* Test PyUCS4_ToUpper() with a small buffer */
265
+ static PyObject *
266
+ unicode_toupper_buffer_too_small (PyObject * self , PyObject * arg )
267
+ {
268
+ Py_UCS4 buf ;
269
+ return unicode_case_operation (arg , PyUCS4_ToUpper , & buf , 1 );
270
+ }
261
271
262
- /* Test PyUnicode_ToLower () */
272
+ /* Test PyUCS4_ToLower () */
263
273
static PyObject *
264
274
unicode_totitle (PyObject * self , PyObject * arg )
265
275
{
266
- return unicode_case_operation (arg , PyUnicode_ToTitle );
276
+ Py_UCS4 buf [PyUCS4_CASE_CONVERSION_BUFFER_SIZE ];
277
+ return unicode_case_operation (arg , PyUCS4_ToTitle , buf , PyUCS4_CASE_CONVERSION_BUFFER_SIZE );
267
278
}
268
279
269
- /* Test PyUnicode_ToLower () */
280
+ /* Test PyUCS4_ToLower () */
270
281
static PyObject *
271
282
unicode_tofolded (PyObject * self , PyObject * arg )
272
283
{
273
- return unicode_case_operation (arg , PyUnicode_ToFolded );
284
+ Py_UCS4 buf [PyUCS4_CASE_CONVERSION_BUFFER_SIZE ];
285
+ return unicode_case_operation (arg , PyUCS4_ToFolded , buf , PyUCS4_CASE_CONVERSION_BUFFER_SIZE );
274
286
}
275
287
276
288
@@ -633,6 +645,7 @@ static PyMethodDef TestMethods[] = {
633
645
{"unicode_GET_CACHED_HASH" , unicode_GET_CACHED_HASH , METH_O },
634
646
{"unicode_tolower" , unicode_tolower , METH_O },
635
647
{"unicode_toupper" , unicode_toupper , METH_O },
648
+ {"unicode_toupper_buffer_too_small" , unicode_toupper_buffer_too_small , METH_O },
636
649
{"unicode_totitle" , unicode_totitle , METH_O },
637
650
{"unicode_tofolded" , unicode_tofolded , METH_O },
638
651
{NULL },
0 commit comments