mirror of
https://github.com/python/cpython.git
synced 2025-10-03 13:45:29 +00:00
_Py_char2wchar() frees the memory on conversion error
Explain in the documentation that conversion errors should never happen.
This commit is contained in:
parent
18c33737f8
commit
19de4c3a8c
1 changed files with 7 additions and 2 deletions
|
@ -16,7 +16,10 @@
|
||||||
Return a pointer to a newly allocated wide character string (use
|
Return a pointer to a newly allocated wide character string (use
|
||||||
PyMem_Free() to free the memory) and write the number of written wide
|
PyMem_Free() to free the memory) and write the number of written wide
|
||||||
characters excluding the null character into *size if size is not NULL, or
|
characters excluding the null character into *size if size is not NULL, or
|
||||||
NULL on error (conversion error or memory error). */
|
NULL on error (conversion or memory allocation error).
|
||||||
|
|
||||||
|
Conversion errors should never happen, unless there is a bug in the C
|
||||||
|
library. */
|
||||||
wchar_t*
|
wchar_t*
|
||||||
_Py_char2wchar(const char* arg, size_t *size)
|
_Py_char2wchar(const char* arg, size_t *size)
|
||||||
{
|
{
|
||||||
|
@ -64,7 +67,8 @@ _Py_char2wchar(const char* arg, size_t *size)
|
||||||
actual output could use less memory. */
|
actual output could use less memory. */
|
||||||
argsize = strlen(arg) + 1;
|
argsize = strlen(arg) + 1;
|
||||||
res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t));
|
res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t));
|
||||||
if (!res) goto oom;
|
if (!res)
|
||||||
|
goto oom;
|
||||||
in = (unsigned char*)arg;
|
in = (unsigned char*)arg;
|
||||||
out = res;
|
out = res;
|
||||||
memset(&mbs, 0, sizeof mbs);
|
memset(&mbs, 0, sizeof mbs);
|
||||||
|
@ -79,6 +83,7 @@ _Py_char2wchar(const char* arg, size_t *size)
|
||||||
unless there is a bug in the C library, or I
|
unless there is a bug in the C library, or I
|
||||||
misunderstood how mbrtowc works. */
|
misunderstood how mbrtowc works. */
|
||||||
fprintf(stderr, "unexpected mbrtowc result -2\n");
|
fprintf(stderr, "unexpected mbrtowc result -2\n");
|
||||||
|
PyMem_Free(res);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (converted == (size_t)-1) {
|
if (converted == (size_t)-1) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue