Skip to content

Commit daafacf

Browse files
ashm-devvstinner
andauthored
pythongh-42400: Fix buffer overflow in _Py_wrealpath() for very long paths (python#141529)
Co-authored-by: Victor Stinner <[email protected]>
1 parent 600f3fe commit daafacf

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix buffer overflow in ``_Py_wrealpath()`` for paths exceeding ``MAXPATHLEN`` bytes
2+
by using dynamic memory allocation instead of fixed-size buffer.
3+
Patch by Shamil Abdulaev.

Python/fileutils.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,7 +2118,6 @@ _Py_wrealpath(const wchar_t *path,
21182118
wchar_t *resolved_path, size_t resolved_path_len)
21192119
{
21202120
char *cpath;
2121-
char cresolved_path[MAXPATHLEN];
21222121
wchar_t *wresolved_path;
21232122
char *res;
21242123
size_t r;
@@ -2127,12 +2126,14 @@ _Py_wrealpath(const wchar_t *path,
21272126
errno = EINVAL;
21282127
return NULL;
21292128
}
2130-
res = realpath(cpath, cresolved_path);
2129+
res = realpath(cpath, NULL);
21312130
PyMem_RawFree(cpath);
21322131
if (res == NULL)
21332132
return NULL;
21342133

2135-
wresolved_path = Py_DecodeLocale(cresolved_path, &r);
2134+
wresolved_path = Py_DecodeLocale(res, &r);
2135+
free(res);
2136+
21362137
if (wresolved_path == NULL) {
21372138
errno = EINVAL;
21382139
return NULL;

0 commit comments

Comments
 (0)