Skip to content

Commit 2ddf7ae

Browse files
author
Victor Stinner
committed
_wrealpath() and _Py_wreadlink() support surrogates (PEP 383)
Use _Py_wchar2char() to support surrogate characters in the input path.
1 parent 3d989e7 commit 2ddf7ae

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

Modules/getpath.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,18 @@ _wgetcwd(wchar_t *buf, size_t size)
179179
int
180180
_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
181181
{
182+
char *cpath;
182183
char cbuf[PATH_MAX];
183-
char cpath[PATH_MAX];
184184
int res;
185-
size_t r1 = wcstombs(cpath, path, PATH_MAX);
186-
if (r1 == (size_t)-1 || r1 >= PATH_MAX) {
185+
size_t r1;
186+
187+
cpath = _Py_wchar2char(path);
188+
if (cpath == NULL) {
187189
errno = EINVAL;
188190
return -1;
189191
}
190192
res = (int)readlink(cpath, cbuf, PATH_MAX);
193+
PyMem_Free(cpath);
191194
if (res == -1)
192195
return -1;
193196
if (res == PATH_MAX) {

Python/sysmodule.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,16 +1661,17 @@ makeargvobject(int argc, wchar_t **argv)
16611661
static wchar_t*
16621662
_wrealpath(const wchar_t *path, wchar_t *resolved_path)
16631663
{
1664-
char cpath[PATH_MAX];
1664+
char *cpath;
16651665
char cresolved_path[PATH_MAX];
16661666
char *res;
16671667
size_t r;
1668-
r = wcstombs(cpath, path, PATH_MAX);
1669-
if (r == (size_t)-1 || r >= PATH_MAX) {
1668+
cpath = _Py_wchar2char(path);
1669+
if (cpath == NULL) {
16701670
errno = EINVAL;
16711671
return NULL;
16721672
}
16731673
res = realpath(cpath, cresolved_path);
1674+
PyMem_Free(cpath);
16741675
if (res == NULL)
16751676
return NULL;
16761677
r = mbstowcs(resolved_path, cresolved_path, PATH_MAX);

0 commit comments

Comments
 (0)