From 9517f69762e4fd36b928b0e145eb880151f36d2b Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 5 Oct 2024 16:43:02 +1000 Subject: [PATCH] Fix BLI_path_is_abs_from_cwd with root/drive-letter paths The paths `C:` (on WIN32) and `/` on other systems was detecting as relative. This meant `BLI_path_abs_from_cwd` would make both paths CWD relative. Also remove duplicate call to BLI_path_is_unc. --- source/blender/blenlib/intern/path_utils.cc | 9 +++++---- source/blender/blenlib/tests/BLI_path_utils_test.cc | 4 ++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/source/blender/blenlib/intern/path_utils.cc b/source/blender/blenlib/intern/path_utils.cc index d036cea254b..897d0f24e73 100644 --- a/source/blender/blenlib/intern/path_utils.cc +++ b/source/blender/blenlib/intern/path_utils.cc @@ -590,7 +590,9 @@ bool BLI_path_is_win32_drive_with_slash(const char *path) */ static bool BLI_path_is_abs_win32(const char *path) { - return BLI_path_is_win32_drive_with_slash(path) || BLI_path_is_unc(path); + /* Don't use the `BLI_path_is_win32_drive_with_slash` + * since paths such as `C:` are valid on their own. */ + return BLI_path_is_win32_drive(path) || BLI_path_is_unc(path); } static wchar_t *next_slash(wchar_t *path) @@ -1199,14 +1201,13 @@ bool BLI_path_abs(char path[FILE_MAX], const char *basepath) bool BLI_path_is_abs_from_cwd(const char *path) { bool is_abs = false; - const int path_len_clamp = BLI_strnlen(path, 3); #ifdef WIN32 - if ((path_len_clamp >= 3 && BLI_path_is_abs_win32(path)) || BLI_path_is_unc(path)) { + if (BLI_path_is_abs_win32(path)) { is_abs = true; } #else - if (path_len_clamp >= 2 && path[0] == '/') { + if (path[0] == '/') { is_abs = true; } #endif diff --git a/source/blender/blenlib/tests/BLI_path_utils_test.cc b/source/blender/blenlib/tests/BLI_path_utils_test.cc index 99a37f28aa3..b44bdba8087 100644 --- a/source/blender/blenlib/tests/BLI_path_utils_test.cc +++ b/source/blender/blenlib/tests/BLI_path_utils_test.cc @@ -1409,14 +1409,18 @@ TEST(path_utils, PathIsAbsFromCwd) { #ifdef WIN32 EXPECT_FALSE(BLI_path_is_abs_from_cwd("/file.txt")); + EXPECT_FALSE(BLI_path_is_abs_from_cwd("/")); + EXPECT_TRUE(BLI_path_is_abs_from_cwd("C:")); EXPECT_TRUE(BLI_path_is_abs_from_cwd("C:/file.txt")); EXPECT_TRUE(BLI_path_is_abs_from_cwd("C:\\file.txt")); EXPECT_TRUE(BLI_path_is_abs_from_cwd("\\\\host\\server\\file.txt")); EXPECT_TRUE(BLI_path_is_abs_from_cwd("\\\\?\\C:\\server\\file.txt")); #else EXPECT_TRUE(BLI_path_is_abs_from_cwd("/file.txt")); + EXPECT_TRUE(BLI_path_is_abs_from_cwd("/")); + EXPECT_FALSE(BLI_path_is_abs_from_cwd("C:")); EXPECT_FALSE(BLI_path_is_abs_from_cwd("C:/file.txt")); EXPECT_FALSE(BLI_path_is_abs_from_cwd("C:\\file.txt")); EXPECT_FALSE(BLI_path_is_abs_from_cwd("\\\\host\\server\\file.txt"));