@@ -2218,11 +2218,11 @@ _Py_normpath(wchar_t *path, Py_ssize_t size)
22182218 if (!path [0 ] || size == 0 ) {
22192219 return path ;
22202220 }
2221- wchar_t lastC = L'\0' ;
2222- wchar_t * p1 = path ;
22232221 wchar_t * pEnd = size >= 0 ? & path [size ] : NULL ;
2224- wchar_t * p2 = path ;
2225- wchar_t * minP2 = path ;
2222+ wchar_t * p1 = path ; // sequentially scanned address in the path
2223+ wchar_t * p2 = path ; // destination of a scanned character to be ljusted
2224+ wchar_t * minP2 = path ; // the beginning of the destination range
2225+ wchar_t lastC = L'\0' ; // the last ljusted character, p2[-1] in most cases
22262226
22272227#define IS_END (x ) (pEnd ? (x) == pEnd : !*(x))
22282228#ifdef ALTSEP
@@ -2264,14 +2264,18 @@ _Py_normpath(wchar_t *path, Py_ssize_t size)
22642264 * p2 ++ = lastC = * p1 ;
22652265 }
22662266 }
2267- minP2 = p2 ;
2267+ if (sepCount ) {
2268+ minP2 = p2 ; // Invalid path
2269+ } else {
2270+ minP2 = p2 - 1 ; // Absolute path has SEP at minP2
2271+ }
22682272 }
22692273#else
22702274 // Skip past two leading SEPs
22712275 else if (IS_SEP (& p1 [0 ]) && IS_SEP (& p1 [1 ]) && !IS_SEP (& p1 [2 ])) {
22722276 * p2 ++ = * p1 ++ ;
22732277 * p2 ++ = * p1 ++ ;
2274- minP2 = p2 ;
2278+ minP2 = p2 - 1 ; // Absolute path has SEP at minP2
22752279 lastC = SEP ;
22762280 }
22772281#endif /* MS_WINDOWS */
@@ -2292,8 +2296,11 @@ _Py_normpath(wchar_t *path, Py_ssize_t size)
22922296 wchar_t * p3 = p2 ;
22932297 while (p3 != minP2 && * -- p3 == SEP ) { }
22942298 while (p3 != minP2 && * (p3 - 1 ) != SEP ) { -- p3 ; }
2295- if (p3 [0 ] == L'.' && p3 [1 ] == L'.' && IS_SEP (& p3 [2 ])) {
2296- // Previous segment is also ../, so append instead
2299+ if (p2 == minP2
2300+ || (p3 [0 ] == L'.' && p3 [1 ] == L'.' && IS_SEP (& p3 [2 ])))
2301+ {
2302+ // Previous segment is also ../, so append instead.
2303+ // Relative path does not absorb ../ at minP2 as well.
22972304 * p2 ++ = L'.' ;
22982305 * p2 ++ = L'.' ;
22992306 lastC = L'.' ;
@@ -2314,7 +2321,7 @@ _Py_normpath(wchar_t *path, Py_ssize_t size)
23142321 }
23152322 } else {
23162323 * p2 ++ = lastC = c ;
2317- }
2324+ }
23182325 }
23192326 * p2 = L'\0' ;
23202327 if (p2 != minP2 ) {
0 commit comments