Skip to content

Commit 3f711f4

Browse files
author
Victor Stinner
committed
_Py_wreadlink() uses _Py_char2wchar() to decode the result, to support
surrogate characters.
1 parent 9d39639 commit 3f711f4

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
@@ -307,6 +307,7 @@ _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
307307
{
308308
char *cpath;
309309
char cbuf[PATH_MAX];
310+
wchar_t *wbuf;
310311
int res;
311312
size_t r1;
312313

@@ -324,11 +325,15 @@ _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
324325
return -1;
325326
}
326327
cbuf[res] = '\0'; /* buf will be null terminated */
327-
r1 = mbstowcs(buf, cbuf, bufsiz);
328-
if (r1 == -1) {
328+
wbuf = _Py_char2wchar(cbuf);
329+
r1 = wcslen(wbuf);
330+
if (bufsiz <= r1) {
331+
PyMem_Free(wbuf);
329332
errno = EINVAL;
330333
return -1;
331334
}
335+
wcsncpy(buf, wbuf, bufsiz);
336+
PyMem_Free(wbuf);
332337
return (int)r1;
333338
}
334339
#endif

0 commit comments

Comments
 (0)