Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19de4c3

Browse files
author
Victor Stinner
committed
_Py_char2wchar() frees the memory on conversion error
Explain in the documentation that conversion errors should never happen.
1 parent 18c3373 commit 19de4c3

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

Python/fileutils.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
Return a pointer to a newly allocated wide character string (use
1717
PyMem_Free() to free the memory) and write the number of written wide
1818
characters excluding the null character into *size if size is not NULL, or
19-
NULL on error (conversion error or memory error). */
19+
NULL on error (conversion or memory allocation error).
20+
21+
Conversion errors should never happen, unless there is a bug in the C
22+
library. */
2023
wchar_t*
2124
_Py_char2wchar(const char* arg, size_t *size)
2225
{
@@ -64,7 +67,8 @@ _Py_char2wchar(const char* arg, size_t *size)
6467
actual output could use less memory. */
6568
argsize = strlen(arg) + 1;
6669
res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t));
67-
if (!res) goto oom;
70+
if (!res)
71+
goto oom;
6872
in = (unsigned char*)arg;
6973
out = res;
7074
memset(&mbs, 0, sizeof mbs);
@@ -79,6 +83,7 @@ _Py_char2wchar(const char* arg, size_t *size)
7983
unless there is a bug in the C library, or I
8084
misunderstood how mbrtowc works. */
8185
fprintf(stderr, "unexpected mbrtowc result -2\n");
86+
PyMem_Free(res);
8287
return NULL;
8388
}
8489
if (converted == (size_t)-1) {

0 commit comments

Comments
 (0)