Mercurial > cpython-lru_cache-weakref
diff Python/fileutils.c @ 66094:76a1c5e88352
_Py_char2wchar() frees the memory on conversion error
Explain in the documentation that conversion errors should never happen.
| author | Victor Stinner <victor.stinner@haypocalc.com> |
|---|---|
| date | Mon, 08 Nov 2010 23:30:46 +0000 |
| parents | 1b5f47e2e0f0 |
| children | c838c9b117f1 94a38ad4aebf |
line wrap: on
line diff
--- a/Python/fileutils.c Mon Nov 08 23:10:20 2010 +0000 +++ b/Python/fileutils.c Mon Nov 08 23:30:46 2010 +0000 @@ -16,7 +16,10 @@ Return a pointer to a newly allocated wide character string (use 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 - 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* _Py_char2wchar(const char* arg, size_t *size) { @@ -64,7 +67,8 @@ actual output could use less memory. */ argsize = strlen(arg) + 1; res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t)); - if (!res) goto oom; + if (!res) + goto oom; in = (unsigned char*)arg; out = res; memset(&mbs, 0, sizeof mbs); @@ -79,6 +83,7 @@ unless there is a bug in the C library, or I misunderstood how mbrtowc works. */ fprintf(stderr, "unexpected mbrtowc result -2\n"); + PyMem_Free(res); return NULL; } if (converted == (size_t)-1) {
