diff options
author | Campbell Barton <campbell@blender.org> | 2022-11-01 13:32:46 +0300 |
---|---|---|
committer | Campbell Barton <campbell@blender.org> | 2022-11-01 23:21:10 +0300 |
commit | 12f4ac170658f82ce85d96873cfb32ec786136ec (patch) | |
tree | 526203955a7d6103cf1024511c83a5d051506e56 | |
parent | 513dfa179f3d7becf2e88da1084741e0c70a8da7 (diff) |
Fix BLI_path_normalize failing with "." and ".." in the path
The logic to go up a directory (using "..") ran before stripping "/./"
from the path. This caused "/a/b/./../" to result in "/a/b/"
instead of "/a/". Now redundant characters are removed before before
checking for ".." in paths.
Include test to ensure this works as expected.
-rw-r--r-- | source/blender/blenlib/intern/path_util.c | 36 | ||||
-rw-r--r-- | source/blender/blenlib/tests/BLI_path_util_test.cc | 6 |
2 files changed, 26 insertions, 16 deletions
diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index b16cf8ea161..df18689c74c 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -153,6 +153,19 @@ void BLI_path_normalize(const char *relabase, char *path) */ #ifdef WIN32 + + while ((start = strstr(path, "\\.\\"))) { + eind = start + strlen("\\.\\") - 1; + memmove(start, eind, strlen(eind) + 1); + } + + /* remove two consecutive backslashes, but skip the UNC prefix, + * which needs to be preserved */ + while ((start = strstr(path + BLI_path_unc_prefix_len(path), "\\\\"))) { + eind = start + strlen("\\\\") - 1; + memmove(start, eind, strlen(eind) + 1); + } + while ((start = strstr(path, "\\..\\"))) { eind = start + strlen("\\..\\") - 1; a = start - path - 1; @@ -170,18 +183,18 @@ void BLI_path_normalize(const char *relabase, char *path) } } - while ((start = strstr(path, "\\.\\"))) { - eind = start + strlen("\\.\\") - 1; +#else + + while ((start = strstr(path, "/./"))) { + eind = start + (3 - 1) /* strlen("/./") - 1 */; memmove(start, eind, strlen(eind) + 1); } - /* remove two consecutive backslashes, but skip the UNC prefix, - * which needs to be preserved */ - while ((start = strstr(path + BLI_path_unc_prefix_len(path), "\\\\"))) { - eind = start + strlen("\\\\") - 1; + while ((start = strstr(path, "//"))) { + eind = start + (2 - 1) /* strlen("//") - 1 */; memmove(start, eind, strlen(eind) + 1); } -#else + while ((start = strstr(path, "/../"))) { a = start - path - 1; if (a > 0) { @@ -206,15 +219,6 @@ void BLI_path_normalize(const char *relabase, char *path) } } - while ((start = strstr(path, "/./"))) { - eind = start + (3 - 1) /* strlen("/./") - 1 */; - memmove(start, eind, strlen(eind) + 1); - } - - while ((start = strstr(path, "//"))) { - eind = start + (2 - 1) /* strlen("//") - 1 */; - memmove(start, eind, strlen(eind) + 1); - } #endif } diff --git a/source/blender/blenlib/tests/BLI_path_util_test.cc b/source/blender/blenlib/tests/BLI_path_util_test.cc index 2f0e730129c..51db376e03f 100644 --- a/source/blender/blenlib/tests/BLI_path_util_test.cc +++ b/source/blender/blenlib/tests/BLI_path_util_test.cc @@ -32,6 +32,12 @@ TEST(path_util, Clean_Dot) NORMALIZE("/./././", "/"); NORMALIZE("/a/./././b/", "/a/b/"); } +/* #BLI_path_normalize: complex "/./" -> "/", "//" -> "/", "./path/../" -> "./". */ +TEST(path_util, Clean_Complex) +{ + NORMALIZE("/a/./b/./c/./.././.././", "/a/"); + NORMALIZE("/a//.//b//.//c//.//..//.//..//.//", "/a/"); +} /* #BLI_path_normalize: "//" -> "/" */ TEST(path_util, Clean_DoubleSlash) { |