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

Skip to content

Commit f4061da

Browse files
author
Victor Stinner
committed
_Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
1 parent 22a351a commit f4061da

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

Modules/getpath.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,12 @@ joinpath(wchar_t *buffer, wchar_t *stuff)
231231
/* copy_absolute requires that path be allocated at least
232232
MAXPATHLEN + 1 bytes and that p be no more than MAXPATHLEN bytes. */
233233
static void
234-
copy_absolute(wchar_t *path, wchar_t *p)
234+
copy_absolute(wchar_t *path, wchar_t *p, size_t pathlen)
235235
{
236236
if (p[0] == SEP)
237237
wcscpy(path, p);
238238
else {
239-
if (!_Py_wgetcwd(path, MAXPATHLEN)) {
239+
if (!_Py_wgetcwd(path, pathlen)) {
240240
/* unable to get the current directory */
241241
wcscpy(path, p);
242242
return;
@@ -251,11 +251,11 @@ copy_absolute(wchar_t *path, wchar_t *p)
251251
static void
252252
absolutize(wchar_t *path)
253253
{
254-
wchar_t buffer[MAXPATHLEN + 1];
254+
wchar_t buffer[MAXPATHLEN+1];
255255

256256
if (path[0] == SEP)
257257
return;
258-
copy_absolute(buffer, path);
258+
copy_absolute(buffer, path, MAXPATHLEN+1);
259259
wcscpy(path, buffer);
260260
}
261261

@@ -295,7 +295,7 @@ search_for_prefix(wchar_t *argv0_path, wchar_t *home)
295295
}
296296

297297
/* Search from argv0_path, until root is found */
298-
copy_absolute(prefix, argv0_path);
298+
copy_absolute(prefix, argv0_path, MAXPATHLEN+1);
299299
do {
300300
n = wcslen(prefix);
301301
joinpath(prefix, lib_python);
@@ -372,7 +372,7 @@ search_for_exec_prefix(wchar_t *argv0_path, wchar_t *home)
372372
}
373373

374374
/* Search from argv0_path, until root is found */
375-
copy_absolute(exec_prefix, argv0_path);
375+
copy_absolute(exec_prefix, argv0_path, MAXPATHLEN+1);
376376
do {
377377
n = wcslen(exec_prefix);
378378
joinpath(exec_prefix, lib_python);

Python/fileutils.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ _Py_wrealpath(const wchar_t *path,
364364
}
365365
#endif
366366

367-
/* Get the current directory. Decode the path from the locale encoding. */
367+
/* Get the current directory. size is the buffer size in wide characters
368+
including the null character. Decode the path from the locale encoding. */
368369

369370
wchar_t*
370371
_Py_wgetcwd(wchar_t *buf, size_t size)
@@ -373,12 +374,19 @@ _Py_wgetcwd(wchar_t *buf, size_t size)
373374
return _wgetcwd(buf, size);
374375
#else
375376
char fname[PATH_MAX];
377+
wchar_t *wname;
378+
376379
if (getcwd(fname, PATH_MAX) == NULL)
377380
return NULL;
378-
if (mbstowcs(buf, fname, size) >= size) {
379-
errno = ERANGE;
381+
wname = _Py_char2wchar(fname);
382+
if (wname == NULL)
383+
return NULL;
384+
if (size <= wcslen(wname)) {
385+
PyMem_Free(wname);
380386
return NULL;
381387
}
388+
wcsncpy(buf, wname, size);
389+
PyMem_Free(wname);
382390
return buf;
383391
#endif
384392
}

0 commit comments

Comments
 (0)