gh-42400: Fix buffer overflow in _Py_wrealpath() for very long paths (#141529)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Shamil 2025-11-18 19:34:58 +03:00 committed by GitHub
parent 600f3feb23
commit daafacf005
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 3 deletions

View file

@ -0,0 +1,3 @@
Fix buffer overflow in ``_Py_wrealpath()`` for paths exceeding ``MAXPATHLEN`` bytes
by using dynamic memory allocation instead of fixed-size buffer.
Patch by Shamil Abdulaev.

View file

@ -2118,7 +2118,6 @@ _Py_wrealpath(const wchar_t *path,
wchar_t *resolved_path, size_t resolved_path_len)
{
char *cpath;
char cresolved_path[MAXPATHLEN];
wchar_t *wresolved_path;
char *res;
size_t r;
@ -2127,12 +2126,14 @@ _Py_wrealpath(const wchar_t *path,
errno = EINVAL;
return NULL;
}
res = realpath(cpath, cresolved_path);
res = realpath(cpath, NULL);
PyMem_RawFree(cpath);
if (res == NULL)
return NULL;
wresolved_path = Py_DecodeLocale(cresolved_path, &r);
wresolved_path = Py_DecodeLocale(res, &r);
free(res);
if (wresolved_path == NULL) {
errno = EINVAL;
return NULL;