From 733f2b0bef93f0c78408d50a5d61f07ebe816c9f Mon Sep 17 00:00:00 2001 From: Steve Kirbach Date: Fri, 28 Jun 2024 11:53:22 -0700 Subject: [PATCH 1/3] use shgetknownfolderpath instead of hand building the path --- Sources/CoreFoundation/CFKnownLocations.c | 31 +++++++---------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/Sources/CoreFoundation/CFKnownLocations.c b/Sources/CoreFoundation/CFKnownLocations.c index 4ed726b891..c5c269342b 100644 --- a/Sources/CoreFoundation/CFKnownLocations.c +++ b/Sources/CoreFoundation/CFKnownLocations.c @@ -18,6 +18,7 @@ #if TARGET_OS_WIN32 #include +#include #endif CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUser user, CFStringRef _Nullable username) { @@ -96,30 +97,16 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs } case _kCFKnownLocationUserCurrent: username = CFGetUserName(); - // fallthrough + break; case _kCFKnownLocationUserByName: { - DWORD size = 0; - GetProfilesDirectoryW(NULL, &size); - - wchar_t* path = (wchar_t*)malloc(size * sizeof(wchar_t)); - GetProfilesDirectoryW(path, &size); - - CFStringRef pathRef = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, path, size - 1); - free(path); - - CFURLRef profilesDir = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, pathRef, kCFURLWindowsPathStyle, true); - CFRelease(pathRef); - - CFURLRef usernameDir = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, profilesDir, username, true); - CFURLRef appdataDir = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, usernameDir, CFSTR("AppData"), true); - location = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, appdataDir, CFSTR("Local"), true); - CFRelease(usernameDir); - CFRelease(appdataDir); + wchar_t* path = NULL; + SHGetKnownFolderPath(&FOLDERID_LocalAppData, 0, NULL, &path); + + CFStringRef userPath = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, path, wcslen(path)); + CoTaskMemFree(path); - CFRelease(profilesDir); - if (user == _kCFKnownLocationUserCurrent) { - CFRelease(username); - } + location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, userPath, kCFURLWindowsPathStyle, true); + CFRelease(userPath); break; } } From 1b2f4297fc0ff48a61948dcaf9ed798c428e8984 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Thu, 17 Apr 2025 13:19:00 +0800 Subject: [PATCH 2/3] WINNT // Patch a fallthrough from `_kCFKnownLocationUserCurrent`. --- Sources/CoreFoundation/CFKnownLocations.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/CoreFoundation/CFKnownLocations.c b/Sources/CoreFoundation/CFKnownLocations.c index c5c269342b..e30447716a 100644 --- a/Sources/CoreFoundation/CFKnownLocations.c +++ b/Sources/CoreFoundation/CFKnownLocations.c @@ -97,7 +97,10 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs } case _kCFKnownLocationUserCurrent: username = CFGetUserName(); - break; + if (!CFEqual(username, CFSTR(""))) { + break; + } + // fallthrough. case _kCFKnownLocationUserByName: { wchar_t* path = NULL; SHGetKnownFolderPath(&FOLDERID_LocalAppData, 0, NULL, &path); From c8aa81770e2d345ed542ed3dcc8da48b04699a1c Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Thu, 17 Apr 2025 13:28:54 +0800 Subject: [PATCH 3/3] chore: comment against the previous commit. --- Sources/CoreFoundation/CFKnownLocations.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/CoreFoundation/CFKnownLocations.c b/Sources/CoreFoundation/CFKnownLocations.c index e30447716a..de65757cd7 100644 --- a/Sources/CoreFoundation/CFKnownLocations.c +++ b/Sources/CoreFoundation/CFKnownLocations.c @@ -97,6 +97,8 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs } case _kCFKnownLocationUserCurrent: username = CFGetUserName(); + // The above API returns a non-NULL CFStringRef. The failed result is an empty string. + // We need to check for that case and fall through to the next case. if (!CFEqual(username, CFSTR(""))) { break; }