Skip to content

Commit 4964737

Browse files
committed
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.
1 parent d6ff215 commit 4964737

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -582,40 +582,28 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
582582
return CFCopyHomeDirectoryURL();
583583
}
584584

585-
CFStringRef home = NULL;
586-
LPUSER_INFO_1 pBuffer = NULL;
587-
DWORD dwEntriesRead = 0;
588-
DWORD dwEntries = 0;
589-
DWORD dwToken = 0;
590-
NTSTATUS nStatus;
591-
do {
592-
nStatus = NetUserEnum(NULL, 1, FILTER_NORMAL_ACCOUNT, (LPBYTE*)&pBuffer,
593-
MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwEntries,
594-
&dwToken);
595-
if (nStatus == NERR_Success || nStatus == ERROR_MORE_DATA) {
596-
for (unsigned uiEntry = 0; !home && uiEntry < dwEntriesRead; ++uiEntry) {
597-
CFStringRef name =
598-
CFStringCreateWithCStringNoCopy(kCFAllocatorSystemDefault,
599-
pBuffer[uiEntry].usri1_name,
600-
kCFStringEncodingUTF16,
601-
kCFAllocatorNull);
602-
603-
if (CFEqual(uName, name)) {
604-
home = pBuffer[uiEntry].usri1_home_dir
605-
? CFStringCreateWithCString(kCFAllocatorSystemDefault,
606-
pBuffer[uiEntry].usri1_home_dir,
607-
kCFStringEncodingUTF16)
608-
: CFSTR("");
609-
}
610-
611-
CFRelease(name);
612-
}
613-
}
614-
NetApiBufferFree(pBuffer);
615-
pBuffer = NULL;
616-
} while (nStatus == ERROR_MORE_DATA);
617-
618-
return home;
585+
CFIndex ulLength = CFStringGetLength(uName);
586+
587+
UniChar *buffer = calloc(ulLength + 1, sizeof(UniChar));
588+
if (buffer == NULL)
589+
return NULL;
590+
CFStringGetCharacters(uName, CFRangeMake(0, ulLength), buffer);
591+
592+
CFURLRef url = NULL;
593+
LPUSER_INFO_1 lpUI1 = NULL;
594+
if (NetUserGetInfo(NULL, buffer, 1, (LPBYTE*)&lpUI1) == NERR_Success) {
595+
CFStringRef path =
596+
CFStringCreateWithCharacters(kCFAllocatorSystemDefault,
597+
lpUI1->usri1_home_dir,
598+
wcslen(lpUI1->usri1_home_dir));
599+
url = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, path,
600+
kCFURLWindowsPathStyle, true);
601+
CFRelease(path);
602+
NetApiBufferFree(lpUI1);
603+
}
604+
free(buffer);
605+
606+
return url;
619607
#else
620608
#error Dont know how to compute users home directories on this platform
621609
#endif

0 commit comments

Comments
 (0)