Skip to content

Commit 3008eb6

Browse files
lysnikolaouvstinner
authored andcommitted
Use Py_ssize_t and don't check overflow in loop
1 parent 625ad47 commit 3008eb6

File tree

4 files changed

+45
-45
lines changed

4 files changed

+45
-45
lines changed

Doc/c-api/unicode.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ These APIs can be used for fast direct character conversions:
307307
possible. This function does not raise exceptions.
308308
309309
310-
.. c:function:: Py_ssize_t PyUnicode_ToLower(Py_UCS4 ch, Py_UCS4 *buffer, int size)
310+
.. c:function:: Py_ssize_t PyUnicode_ToLower(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)
311311
312312
Convert *ch* to lower case, store result in *buffer*, which should be
313313
able to hold as many characters needed for *ch* to be lower cased
@@ -318,7 +318,7 @@ These APIs can be used for fast direct character conversions:
318318
.. versionadded:: next
319319
320320
321-
.. c:function:: Py_ssize_t PyUnicode_ToUpper(Py_UCS4 ch, Py_UCS4 *buffer, int size)
321+
.. c:function:: Py_ssize_t PyUnicode_ToUpper(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)
322322
323323
Convert *ch* to upper case, store result in *buffer*, which should be
324324
able to hold as many characters needed for *ch* to be upper cased
@@ -329,7 +329,7 @@ These APIs can be used for fast direct character conversions:
329329
.. versionadded:: next
330330
331331
332-
.. c:function:: Py_ssize_t PyUnicode_ToTitle(Py_UCS4 ch, Py_UCS4 *buffer, int size)
332+
.. c:function:: Py_ssize_t PyUnicode_ToTitle(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)
333333
334334
Convert *ch* to title case, store result in *buffer*, which should be
335335
able to hold as many characters needed for *ch* to be title cased
@@ -340,7 +340,7 @@ These APIs can be used for fast direct character conversions:
340340
.. versionadded:: next
341341
342342
343-
.. c:function:: Py_ssize_t PyUnicode_ToFolded(Py_UCS4 ch, Py_UCS4 *buffer, int size)
343+
.. c:function:: Py_ssize_t PyUnicode_ToFolded(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)
344344
345345
Foldcase *ch*, store result in *buffer*, which should be
346346
able to hold as many characters needed for *ch* to be foldcased

Include/cpython/unicodeobject.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -733,28 +733,28 @@ PyAPI_FUNC(int) _PyUnicode_IsAlpha(
733733
Py_UCS4 ch /* Unicode character */
734734
);
735735

736-
PyAPI_FUNC(int) PyUnicode_ToLower(
736+
PyAPI_FUNC(Py_ssize_t) PyUnicode_ToLower(
737737
Py_UCS4 ch, /* Unicode character */
738738
Py_UCS4 *res, /* Output buffer */
739-
int size /* Buffer size */
739+
Py_ssize_t size /* Buffer size */
740740
);
741741

742-
PyAPI_FUNC(int) PyUnicode_ToUpper(
742+
PyAPI_FUNC(Py_ssize_t) PyUnicode_ToUpper(
743743
Py_UCS4 ch, /* Unicode character */
744744
Py_UCS4 *res, /* Output buffer */
745-
int size /* Buffer size */
745+
Py_ssize_t size /* Buffer size */
746746
);
747747

748-
PyAPI_FUNC(int) PyUnicode_ToTitle(
748+
PyAPI_FUNC(Py_ssize_t) PyUnicode_ToTitle(
749749
Py_UCS4 ch, /* Unicode character */
750750
Py_UCS4 *res, /* Output buffer */
751-
int size /* Buffer size */
751+
Py_ssize_t size /* Buffer size */
752752
);
753753

754-
PyAPI_FUNC(int) PyUnicode_ToFolded(
754+
PyAPI_FUNC(Py_ssize_t) PyUnicode_ToFolded(
755755
Py_UCS4 ch, /* Unicode character */
756756
Py_UCS4 *res, /* Output buffer */
757-
int size /* Buffer size */
757+
Py_ssize_t size /* Buffer size */
758758
);
759759

760760

Modules/_testcapi/unicode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ unicode_copycharacters(PyObject *self, PyObject *args)
221221
}
222222

223223
static PyObject *
224-
unicode_case_operation(PyObject *str, int (*function)(Py_UCS4, Py_UCS4 *, int))
224+
unicode_case_operation(PyObject *str, Py_ssize_t (*function)(Py_UCS4, Py_UCS4 *, Py_ssize_t))
225225
{
226226
if (!PyUnicode_Check(str)) {
227227
PyErr_Format(PyExc_TypeError, "expect str type, got %T", str);
@@ -236,7 +236,7 @@ unicode_case_operation(PyObject *str, int (*function)(Py_UCS4, Py_UCS4 *, int))
236236
Py_UCS4 c = PyUnicode_READ_CHAR(str, 0);
237237

238238
Py_UCS4 buf[3];
239-
int chars = function(c, buf, Py_ARRAY_LENGTH(buf));
239+
Py_ssize_t chars = function(c, buf, Py_ARRAY_LENGTH(buf));
240240
if (chars < 0) {
241241
return NULL;
242242
}

Objects/unicodectype.c

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)