From 49647378674c4528b5cee511bdd16f58d09a1f86 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Thu, 19 Jan 2023 14:22:11 -0800 Subject: [PATCH] Base.subproject: correct home directory handling on Windows Prefer to use `NetUserGetInfo` over enumerating all users as that is unnecessary. Correct the string handling where we used `CFStringCreateWithBytesNoCopy` which expects the input to be in an 8-bit encoding which we do not use (nor does Windows support for the SAM access). This corrects the string manipulation which improves the test suite pass rate though some errors still remain. --- CoreFoundation/Base.subproj/CFPlatform.c | 56 ++++++++++-------------- 1 file changed, 22 insertions(+), 34 deletions(-) diff --git a/CoreFoundation/Base.subproj/CFPlatform.c b/CoreFoundation/Base.subproj/CFPlatform.c index b4a274568f..9cebdfdab3 100644 --- a/CoreFoundation/Base.subproj/CFPlatform.c +++ b/CoreFoundation/Base.subproj/CFPlatform.c @@ -582,40 +582,28 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) { return CFCopyHomeDirectoryURL(); } - CFStringRef home = NULL; - LPUSER_INFO_1 pBuffer = NULL; - DWORD dwEntriesRead = 0; - DWORD dwEntries = 0; - DWORD dwToken = 0; - NTSTATUS nStatus; - do { - nStatus = NetUserEnum(NULL, 1, FILTER_NORMAL_ACCOUNT, (LPBYTE*)&pBuffer, - MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwEntries, - &dwToken); - if (nStatus == NERR_Success || nStatus == ERROR_MORE_DATA) { - for (unsigned uiEntry = 0; !home && uiEntry < dwEntriesRead; ++uiEntry) { - CFStringRef name = - CFStringCreateWithCStringNoCopy(kCFAllocatorSystemDefault, - pBuffer[uiEntry].usri1_name, - kCFStringEncodingUTF16, - kCFAllocatorNull); - - if (CFEqual(uName, name)) { - home = pBuffer[uiEntry].usri1_home_dir - ? CFStringCreateWithCString(kCFAllocatorSystemDefault, - pBuffer[uiEntry].usri1_home_dir, - kCFStringEncodingUTF16) - : CFSTR(""); - } - - CFRelease(name); - } - } - NetApiBufferFree(pBuffer); - pBuffer = NULL; - } while (nStatus == ERROR_MORE_DATA); - - return home; + CFIndex ulLength = CFStringGetLength(uName); + + UniChar *buffer = calloc(ulLength + 1, sizeof(UniChar)); + if (buffer == NULL) + return NULL; + CFStringGetCharacters(uName, CFRangeMake(0, ulLength), buffer); + + CFURLRef url = NULL; + LPUSER_INFO_1 lpUI1 = NULL; + if (NetUserGetInfo(NULL, buffer, 1, (LPBYTE*)&lpUI1) == NERR_Success) { + CFStringRef path = + CFStringCreateWithCharacters(kCFAllocatorSystemDefault, + lpUI1->usri1_home_dir, + wcslen(lpUI1->usri1_home_dir)); + url = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, path, + kCFURLWindowsPathStyle, true); + CFRelease(path); + NetApiBufferFree(lpUI1); + } + free(buffer); + + return url; #else #error Dont know how to compute users home directories on this platform #endif