diff --git a/Modules/getpath.c b/Modules/getpath.c index 10ad509157a..418c683ce66 100644 --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -190,10 +190,14 @@ isdir(char *filename) /* Is directory */ } -/* joinpath requires that any buffer argument passed to it has at - least MAXPATHLEN + 1 bytes allocated. If this requirement is met, - it guarantees that it will never overflow the buffer. If stuff - is too long, buffer will contain a truncated copy of stuff. +/* Add a path component, by appending stuff to buffer. + buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a + NUL-terminated string with no more than MAXPATHLEN characters (not counting + the trailing NUL). It's a fatal error if it contains a string longer than + that (callers must be careful!). If these requirements are met, it's + guaranteed that buffer will still be a NUL-terminated string with no more + than MAXPATHLEN characters at exit. If stuff is too long, only as much of + stuff as fits will be appended. */ static void joinpath(char *buffer, char *stuff) @@ -206,6 +210,8 @@ joinpath(char *buffer, char *stuff) if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN) buffer[n++] = SEP; } + if (n > MAXPATHLEN) + Py_FatalError("buffer overflow in getpath.c's joinpath()"); k = strlen(stuff); if (n + k > MAXPATHLEN) k = MAXPATHLEN - n;