Skip to content

Commit 769d84a

Browse files
lysnikolaouvstinner
authored andcommitted
Disallow passing NULL
1 parent 5979fdb commit 769d84a

File tree

2 files changed

+39
-54
lines changed

2 files changed

+39
-54
lines changed

Doc/c-api/unicode.rst

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,8 @@ These APIs can be used for fast direct character conversions:
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
314314
(e.g. a maximum of two character for Unicode 16.0), and
315-
return the number of characters stored. Passing a ``NULL`` buffer returns
316-
the buffer size needed. If at some point a buffer overflow is detected,
317-
an :exc:`ValueError` is raised and ``-1`` is returned.
315+
return the number of characters stored. If at some point a buffer overflow
316+
is detected, an :exc:`ValueError` is raised and ``-1`` is returned.
318317
319318
.. versionadded:: next
320319
@@ -324,9 +323,8 @@ These APIs can be used for fast direct character conversions:
324323
Convert *ch* to upper case, store result in *buffer*, which should be
325324
able to hold as many characters needed for *ch* to be upper cased
326325
(e.g. a maximum of three character for Unicode 16.0), and
327-
return the number of characters stored. Passing a ``NULL`` buffer returns
328-
the buffer size needed. If at some point a buffer overflow is detected,
329-
an :exc:`ValueError` is raised and ``-1`` is returned.
326+
return the number of characters stored. If at some point a buffer overflow
327+
is detected, an :exc:`ValueError` is raised and ``-1`` is returned.
330328
331329
.. versionadded:: next
332330
@@ -336,9 +334,8 @@ These APIs can be used for fast direct character conversions:
336334
Convert *ch* to title case, store result in *buffer*, which should be
337335
able to hold as many characters needed for *ch* to be title cased
338336
(e.g. a maximum of three character for Unicode 16.0), and
339-
return the number of characters stored. Passing a ``NULL`` buffer returns
340-
the buffer size needed. If at some point a buffer overflow is detected,
341-
an :exc:`ValueError` is raised and ``-1`` is returned.
337+
return the number of characters stored. If at some point a buffer overflow
338+
is detected, an :exc:`ValueError` is raised and ``-1`` is returned.
342339
343340
.. versionadded:: next
344341
@@ -348,9 +345,8 @@ These APIs can be used for fast direct character conversions:
348345
Foldcase *ch*, store result in *buffer*, which should be
349346
able to hold as many characters needed for *ch* to be foldcased
350347
(e.g. a maximum of three character for Unicode 16.0), and
351-
return the number of characters stored. Passing a ``NULL`` buffer returns
352-
the buffer size needed. If at some point a buffer overflow is detected,
353-
an :exc:`ValueError` is raised and ``-1`` is returned.
348+
return the number of characters stored. If at some point a buffer overflow
349+
is detected, an :exc:`ValueError` is raised and ``-1`` is returned.
354350
355351
.. versionadded:: next
356352

Objects/unicodectype.c

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -207,24 +207,20 @@ int PyUnicode_ToLower(Py_UCS4 ch, Py_UCS4 *res, int size)
207207
int n = ctype->lower >> 24;
208208
int i;
209209
for (i = 0; i < n; i++) {
210-
if (res != NULL) {
211-
if (i >= size) {
212-
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
213-
return -1;
214-
}
215-
res[i] = _PyUnicode_ExtendedCase[index + i];
210+
if (i >= size) {
211+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
212+
return -1;
216213
}
214+
res[i] = _PyUnicode_ExtendedCase[index + i];
217215
}
218216
return n;
219217
}
220218

221-
if (res != NULL) {
222-
if (0 >= size) {
223-
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
224-
return -1;
225-
}
226-
res[0] = ch + ctype->lower;
219+
if (0 >= size) {
220+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
221+
return -1;
227222
}
223+
res[0] = ch + ctype->lower;
228224
return 1;
229225
}
230226

@@ -237,23 +233,20 @@ int PyUnicode_ToTitle(Py_UCS4 ch, Py_UCS4 *res, int size)
237233
int n = ctype->title >> 24;
238234
int i;
239235
for (i = 0; i < n; i++) {
240-
if (res != NULL) {
241-
if (i >= size) {
242-
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
243-
return -1;
244-
}
245-
res[i] = _PyUnicode_ExtendedCase[index + i];
236+
if (i >= size) {
237+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
238+
return -1;
246239
}
240+
res[i] = _PyUnicode_ExtendedCase[index + i];
247241
}
248242
return n;
249243
}
250-
if (res != NULL) {
251-
if (0 >= size) {
252-
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
253-
return -1;
254-
}
255-
res[0] = ch + ctype->title;
244+
245+
if (0 >= size) {
246+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
247+
return -1;
256248
}
249+
res[0] = ch + ctype->title;
257250
return 1;
258251
}
259252

@@ -266,23 +259,20 @@ int PyUnicode_ToUpper(Py_UCS4 ch, Py_UCS4 *res, int size)
266259
int n = ctype->upper >> 24;
267260
int i;
268261
for (i = 0; i < n; i++) {
269-
if (res != NULL) {
270-
if (i >= size) {
271-
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
272-
return -1;
273-
}
274-
res[i] = _PyUnicode_ExtendedCase[index + i];
262+
if (i >= size) {
263+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
264+
return -1;
275265
}
266+
res[i] = _PyUnicode_ExtendedCase[index + i];
276267
}
277268
return n;
278269
}
279-
if (res != NULL) {
280-
if (0 >= size) {
281-
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
282-
return -1;
283-
}
284-
res[0] = ch + ctype->upper;
270+
271+
if (0 >= size) {
272+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
273+
return -1;
285274
}
275+
res[0] = ch + ctype->upper;
286276
return 1;
287277
}
288278

@@ -295,16 +285,15 @@ int PyUnicode_ToFolded(Py_UCS4 ch, Py_UCS4 *res, int size)
295285
int n = (ctype->lower >> 20) & 7;
296286
int i;
297287
for (i = 0; i < n; i++) {
298-
if (res != NULL) {
299-
if (i >= size) {
300-
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
301-
return -1;
302-
}
303-
res[i] = _PyUnicode_ExtendedCase[index + i];
288+
if (i >= size) {
289+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
290+
return -1;
304291
}
292+
res[i] = _PyUnicode_ExtendedCase[index + i];
305293
}
306294
return n;
307295
}
296+
308297
return PyUnicode_ToLower(ch, res, size);
309298
}
310299

0 commit comments

Comments
 (0)