Skip to content

Commit 78d865e

Browse files
committed
Modified code changes to address the review comments.
1 parent 3edc9c5 commit 78d865e

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

CoreFoundation/Base.subproj/CFFileUtilities.c

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,14 +1221,20 @@ CFArrayRef _CFXDGCreateDataDirectoriesPaths(void) {
12211221
// $XDG_DATA_DIRS defines the preference-ordered set of base directories to search for data files in addition to the $XDG_DATA_HOME base directory. The directories in $XDG_DATA_DIRS should be seperated with a colon ':'.
12221222
// If $XDG_DATA_DIRS is either not set or empty, a value equal to /usr/local/share/:/usr/share/ should be used.
12231223
const char *dataDirectoriesPaths = __CFgetenv("XDG_DATA_DIRS");
1224+
CFStringRef defaultPath[2];
1225+
defaultPath[0] = CFSTR("/usr/local/share");
1226+
defaultPath[1] = CFSTR("/usr/share");
12241227
if ((dataDirectoriesPaths == NULL) || (dataDirectoriesPaths[0] == '\0')) {
12251228
// Environmental variable not set. Return default value.
1226-
CFStringRef defaultPath[2];
1227-
defaultPath[0] = CFStringCreateWithCString(kCFAllocatorSystemDefault, "/usr/local/share/", _kCFXDGStringEncoding);
1228-
defaultPath[1] = CFStringCreateWithCString(kCFAllocatorSystemDefault, "/usr/share/", _kCFXDGStringEncoding);
12291229
return CFArrayCreate(kCFAllocatorSystemDefault, (const void **)defaultPath, 2, &kCFTypeArrayCallBacks);
12301230
}
1231-
return _CFTokenizeStringToCFArrayOfCFStrings(dataDirectoriesPaths, ':');
1231+
CFArrayRef dataDirPathsArray = _CFCreateCFArrayByTokenizingString(dataDirectoriesPaths, ':');
1232+
if(CFArrayGetCount(dataDirPathsArray) == 0) {
1233+
CFStringRef logMessage = CFSTR("Value set in XDG_DATA_DIRS variable not honoured. Returning the default.");
1234+
CFLog(kCFLogLevelWarning, CFSTR("%@"), logMessage);
1235+
return CFArrayCreate(kCFAllocatorSystemDefault, (const void **)defaultPath, 2, &kCFTypeArrayCallBacks);
1236+
}
1237+
return dataDirPathsArray;
12321238
}
12331239

12341240

@@ -1238,13 +1244,19 @@ CFArrayRef _CFXDGCreateConfigDirectoriesPaths(void) {
12381244
// $XDG_CONFIG_DIRS defines the preference-ordered set of base directories to search for configuration files in addition to the $XDG_CONFIG_HOME base directory. The directories in $XDG_CONFIG_DIRS should be seperated with a colon ':'.
12391245
// If $XDG_CONFIG_DIRS is either not set or empty, a value equal to /etc/xdg should be used.
12401246
const char *configDirectoriesPaths = __CFgetenv("XDG_CONFIG_DIRS");
1247+
CFStringRef defaultPath[1];
1248+
defaultPath[0] = CFSTR("/etc/xdg");
12411249
if ((configDirectoriesPaths == NULL) || (configDirectoriesPaths[0] == '\0')) {
12421250
//Environmental variable not set. Return default value.
1243-
CFStringRef defaultPath[1];
1244-
defaultPath[0] = CFStringCreateWithCString(kCFAllocatorSystemDefault, "/etc/xdg", _kCFXDGStringEncoding);
12451251
return CFArrayCreate(kCFAllocatorSystemDefault, (const void **)defaultPath, 1, &kCFTypeArrayCallBacks);
12461252
}
1247-
return _CFTokenizeStringToCFArrayOfCFStrings(configDirectoriesPaths, ':');
1253+
CFArrayRef configDirPathsArray = _CFCreateCFArrayByTokenizingString(configDirectoriesPaths, ':');
1254+
if(CFArrayGetCount(configDirPathsArray) == 0) {
1255+
CFStringRef logMessage = CFSTR("Value set in XDG_CONFIG_DIRS variable not honoured. Returning the default.");
1256+
CFLog(kCFLogLevelWarning, CFSTR("%@"), logMessage);
1257+
return CFArrayCreate(kCFAllocatorSystemDefault, (const void **)defaultPath, 1, &kCFTypeArrayCallBacks);
1258+
}
1259+
return configDirPathsArray;
12481260
}
12491261

12501262
/// a single base directory relative to which user-specific non-essential (cached) data should be written. This directory is defined by the environment variable $XDG_CACHE_HOME.
@@ -1276,7 +1288,7 @@ CFStringRef _CFXDGCreateRuntimeDirectoryPath(void) {
12761288
}
12771289

12781290

1279-
CF_PRIVATE CFArrayRef _CFTokenizeStringToCFArrayOfCFStrings(const char *values, char delimiter) {
1291+
CF_PRIVATE CFArrayRef _CFCreateCFArrayByTokenizingString(const char *values, char delimiter) {
12801292
size_t pathCount = 0;
12811293
char* tmpDirectoriesPaths = (char*)values;
12821294
char* last_colon = 0;
@@ -1292,6 +1304,9 @@ CF_PRIVATE CFArrayRef _CFTokenizeStringToCFArrayOfCFStrings(const char *values,
12921304
}
12931305
// Add count for trailing path unless ending with colon.
12941306
pathCount += last_colon < (values + strlen(values) - 1);
1307+
if (pathCount > 64) {
1308+
return CFArrayCreate(kCFAllocatorSystemDefault, NULL, 0, &kCFTypeArrayCallBacks);
1309+
}
12951310
if (pathCount > 0)
12961311
{
12971312
size_t validPathCount = 0;
@@ -1304,16 +1319,25 @@ CF_PRIVATE CFArrayRef _CFTokenizeStringToCFArrayOfCFStrings(const char *values,
13041319
while (path)
13051320
{
13061321
assert(validPathCount < pathCount);
1307-
CFStringRef dirPath = CFStringCreateWithCString(kCFAllocatorSystemDefault, strdup(path), _kCFXDGStringEncoding);
1322+
char* pathString = strdup(path);
1323+
CFStringRef dirPath = CFStringCreateWithCString(kCFAllocatorSystemDefault, pathString, _kCFXDGStringEncoding);
13081324
CFStringRef slash = CFSTR("/");
13091325
CFStringRef tilde = CFSTR("~");
13101326
// Check for absolutePath, if not ignore.
13111327
if (CFStringHasPrefix(dirPath, slash) || CFStringHasPrefix(dirPath, tilde) ) {
13121328
pathList[validPathCount++] = dirPath;
1329+
} else {
1330+
CFRelease(dirPath);
13131331
}
1314-
path = strtok(0, delimiterStr);
1332+
path = strtok(NULL, delimiterStr);
1333+
free(pathString);
1334+
}
1335+
free(copyDirPath);
1336+
CFArrayRef pathArray = CFArrayCreate(kCFAllocatorSystemDefault, (const void **)pathList , validPathCount, &kCFTypeArrayCallBacks);
1337+
for(int i = 0; i < validPathCount; i ++) {
1338+
CFRelease(pathList[i]);
13151339
}
1316-
return CFArrayCreate(kCFAllocatorSystemDefault, (const void **)pathList, validPathCount, &kCFTypeArrayCallBacks);
1340+
return pathArray;
13171341
}
13181342
return CFArrayCreate(kCFAllocatorSystemDefault, NULL, 0, &kCFTypeArrayCallBacks);
13191343
}

CoreFoundation/Base.subproj/CFInternal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ CF_PRIVATE CFIndex _CFLengthAfterDeletingPathExtension2(CFStringRef path);
599599
CF_EXPORT CFIndex _CFStartOfPathExtension(UniChar *unichars, CFIndex length);
600600
CF_PRIVATE CFIndex _CFStartOfPathExtension2(CFStringRef path);
601601
CF_EXPORT CFIndex _CFLengthAfterDeletingPathExtension(UniChar *unichars, CFIndex length);
602-
CF_PRIVATE CFArrayRef _CFTokenizeStringToCFArrayOfCFStrings(const char *values, char delimiter);
602+
CF_PRIVATE CFArrayRef _CFCreateCFArrayByTokenizingString(const char *values, char delimiter);
603603

604604
#if __BLOCKS__
605605
#if DEPLOYMENT_TARGET_WINDOWS

TestFoundation/XDGTestHelper.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Foundation
2-
import XCTest
32

43
let storage = HTTPCookieStorage.shared
54
let simpleCookie = HTTPCookie(properties: [
@@ -11,9 +10,10 @@ let simpleCookie = HTTPCookie(properties: [
1110
let rawValue = getenv("XDG_CONFIG_HOME")
1211
let xdg_config_home = String(utf8String: rawValue!)
1312
storage.setCookie(simpleCookie)
14-
XCTAssertEqual(storage.cookies!.count, 1)
1513
let fm = FileManager.default
1614
let destPath = xdg_config_home! + "/.cookies.shared"
1715
var isDir = false
1816
let exists = fm.fileExists(atPath: destPath, isDirectory: &isDir)
19-
XCTAssertTrue(exists)
17+
if (!exists) {
18+
exit(-1)
19+
}

0 commit comments

Comments
 (0)