Skip to content

Commit 16e6a80

Browse files
author
Victor Stinner
committed
PyUnicode_Resize(): warn about canonical representation
Call also directly unicode_resize() in unicodeobject.c
1 parent b0a82a6 commit 16e6a80

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

Include/unicodeobject.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,10 @@ PyAPI_FUNC(Py_UNICODE) PyUnicode_GetMax(void);
779779
a new string and copy characters), or create a new string.
780780
781781
Error handling is implemented as follows: an exception is set, -1
782-
is returned and *unicode left untouched. */
782+
is returned and *unicode left untouched.
783+
784+
WARNING: The function doesn't check string content, the result may not be a
785+
string in canonical representation. */
783786

784787
PyAPI_FUNC(int) PyUnicode_Resize(
785788
PyObject **unicode, /* Pointer to the Unicode object */

Objects/unicodeobject.c

+13-12
Original file line numberDiff line numberDiff line change
@@ -5040,7 +5040,7 @@ PyUnicode_DecodeUTF32Stateful(const char *s,
50405040
*consumed = (const char *)q-starts;
50415041

50425042
/* Adjust length */
5043-
if (PyUnicode_Resize(&unicode, outpos) < 0)
5043+
if (unicode_resize(&unicode, outpos) < 0)
50445044
goto onError;
50455045

50465046
Py_XDECREF(errorHandler);
@@ -5404,7 +5404,7 @@ PyUnicode_DecodeUTF16Stateful(const char *s,
54045404
*consumed = (const char *)q-starts;
54055405

54065406
/* Adjust length */
5407-
if (PyUnicode_Resize(&unicode, outpos) < 0)
5407+
if (unicode_resize(&unicode, outpos) < 0)
54085408
goto onError;
54095409

54105410
Py_XDECREF(errorHandler);
@@ -5824,7 +5824,7 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
58245824
}
58255825
#undef WRITECHAR
58265826

5827-
if (PyUnicode_Resize(&v, i) < 0)
5827+
if (unicode_resize(&v, i) < 0)
58285828
goto onError;
58295829
Py_XDECREF(errorHandler);
58305830
Py_XDECREF(exc);
@@ -6086,7 +6086,7 @@ PyUnicode_DecodeRawUnicodeEscape(const char *s,
60866086
nextByte:
60876087
;
60886088
}
6089-
if (PyUnicode_Resize(&v, outpos) < 0)
6089+
if (unicode_resize(&v, outpos) < 0)
60906090
goto onError;
60916091
Py_XDECREF(errorHandler);
60926092
Py_XDECREF(exc);
@@ -6273,7 +6273,7 @@ _PyUnicode_DecodeUnicodeInternal(const char *s,
62736273
goto onError;
62746274
}
62756275

6276-
if (PyUnicode_Resize(&v, outpos) < 0)
6276+
if (unicode_resize(&v, outpos) < 0)
62776277
goto onError;
62786278
Py_XDECREF(errorHandler);
62796279
Py_XDECREF(exc);
@@ -6727,7 +6727,7 @@ PyUnicode_DecodeASCII(const char *s,
67276727
data = PyUnicode_DATA(v);
67286728
}
67296729
}
6730-
if (PyUnicode_Resize(&v, outpos) < 0)
6730+
if (unicode_resize(&v, outpos) < 0)
67316731
goto onError;
67326732
Py_XDECREF(errorHandler);
67336733
Py_XDECREF(exc);
@@ -6874,7 +6874,7 @@ decode_code_page_strict(UINT code_page,
68746874
else {
68756875
/* Extend unicode object */
68766876
Py_ssize_t n = PyUnicode_GET_SIZE(*v);
6877-
if (PyUnicode_Resize(v, n + outsize) < 0)
6877+
if (unicode_resize(v, n + outsize) < 0)
68786878
return -1;
68796879
out = PyUnicode_AS_UNICODE(*v) + n;
68806880
}
@@ -6958,7 +6958,7 @@ decode_code_page_errors(UINT code_page,
69586958
PyErr_NoMemory();
69596959
goto error;
69606960
}
6961-
if (PyUnicode_Resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0)
6961+
if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0)
69626962
goto error;
69636963
startout = PyUnicode_AS_UNICODE(*v) + n;
69646964
}
@@ -7017,7 +7017,7 @@ decode_code_page_errors(UINT code_page,
70177017
/* Extend unicode object */
70187018
outsize = out - startout;
70197019
assert(outsize <= PyUnicode_WSTR_LENGTH(*v));
7020-
if (PyUnicode_Resize(v, outsize) < 0)
7020+
if (unicode_resize(v, outsize) < 0)
70217021
goto error;
70227022
ret = size;
70237023

@@ -7664,8 +7664,9 @@ PyUnicode_DecodeCharmap(const char *s,
76647664
(targetsize << 2);
76657665
extrachars += needed;
76667666
/* XXX overflow detection missing */
7667-
if (PyUnicode_Resize(&v,
7668-
PyUnicode_GET_LENGTH(v) + needed) < 0) {
7667+
if (unicode_resize(&v,
7668+
PyUnicode_GET_LENGTH(v) + needed) < 0)
7669+
{
76697670
Py_DECREF(x);
76707671
goto onError;
76717672
}
@@ -7689,7 +7690,7 @@ PyUnicode_DecodeCharmap(const char *s,
76897690
++s;
76907691
}
76917692
}
7692-
if (PyUnicode_Resize(&v, outpos) < 0)
7693+
if (unicode_resize(&v, outpos) < 0)
76937694
goto onError;
76947695
Py_XDECREF(errorHandler);
76957696
Py_XDECREF(exc);

0 commit comments

Comments
 (0)