@@ -1221,14 +1221,21 @@ CFArrayRef _CFXDGCreateDataDirectoriesPaths(void) {
1221
1221
// $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 ':'.
1222
1222
// If $XDG_DATA_DIRS is either not set or empty, a value equal to /usr/local/share/:/usr/share/ should be used.
1223
1223
const char * dataDirectoriesPaths = __CFgetenv ("XDG_DATA_DIRS" );
1224
+ CFStringRef defaultPath [2 ];
1225
+ defaultPath [0 ] = CFSTR ("/usr/local/share" );
1226
+ defaultPath [1 ] = CFSTR ("/usr/share" );
1224
1227
if ((dataDirectoriesPaths == NULL ) || (dataDirectoriesPaths [0 ] == '\0' )) {
1225
1228
// 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 );
1229
1229
return CFArrayCreate (kCFAllocatorSystemDefault , (const void * * )defaultPath , 2 , & kCFTypeArrayCallBacks );
1230
1230
}
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
+ CFRelease (dataDirPathsArray );
1236
+ return CFArrayCreate (kCFAllocatorSystemDefault , (const void * * )defaultPath , 2 , & kCFTypeArrayCallBacks );
1237
+ }
1238
+ return dataDirPathsArray ;
1232
1239
}
1233
1240
1234
1241
@@ -1238,13 +1245,20 @@ CFArrayRef _CFXDGCreateConfigDirectoriesPaths(void) {
1238
1245
// $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 ':'.
1239
1246
// If $XDG_CONFIG_DIRS is either not set or empty, a value equal to /etc/xdg should be used.
1240
1247
const char * configDirectoriesPaths = __CFgetenv ("XDG_CONFIG_DIRS" );
1248
+ CFStringRef defaultPath [1 ];
1249
+ defaultPath [0 ] = CFSTR ("/etc/xdg" );
1241
1250
if ((configDirectoriesPaths == NULL ) || (configDirectoriesPaths [0 ] == '\0' )) {
1242
1251
//Environmental variable not set. Return default value.
1243
- CFStringRef defaultPath [1 ];
1244
- defaultPath [0 ] = CFStringCreateWithCString (kCFAllocatorSystemDefault , "/etc/xdg" , _kCFXDGStringEncoding );
1245
1252
return CFArrayCreate (kCFAllocatorSystemDefault , (const void * * )defaultPath , 1 , & kCFTypeArrayCallBacks );
1246
1253
}
1247
- return _CFTokenizeStringToCFArrayOfCFStrings (configDirectoriesPaths , ':' );
1254
+ CFArrayRef configDirPathsArray = _CFCreateCFArrayByTokenizingString (configDirectoriesPaths , ':' );
1255
+ if (CFArrayGetCount (configDirPathsArray ) == 0 ) {
1256
+ CFStringRef logMessage = CFSTR ("Value set in XDG_CONFIG_DIRS variable not honoured. Returning the default." );
1257
+ CFLog (kCFLogLevelWarning , CFSTR ("%@" ), logMessage );
1258
+ CFRelease (configDirPathsArray );
1259
+ return CFArrayCreate (kCFAllocatorSystemDefault , (const void * * )defaultPath , 1 , & kCFTypeArrayCallBacks );
1260
+ }
1261
+ return configDirPathsArray ;
1248
1262
}
1249
1263
1250
1264
/// 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 +1290,7 @@ CFStringRef _CFXDGCreateRuntimeDirectoryPath(void) {
1276
1290
}
1277
1291
1278
1292
1279
- CF_PRIVATE CFArrayRef _CFTokenizeStringToCFArrayOfCFStrings (const char * values , char delimiter ) {
1293
+ CF_PRIVATE CFArrayRef _CFCreateCFArrayByTokenizingString (const char * values , char delimiter ) {
1280
1294
size_t pathCount = 0 ;
1281
1295
char * tmpDirectoriesPaths = (char * )values ;
1282
1296
char * last_colon = 0 ;
@@ -1292,6 +1306,9 @@ CF_PRIVATE CFArrayRef _CFTokenizeStringToCFArrayOfCFStrings(const char *values,
1292
1306
}
1293
1307
// Add count for trailing path unless ending with colon.
1294
1308
pathCount += last_colon < (values + strlen (values ) - 1 );
1309
+ if (pathCount > 64 ) {
1310
+ return CFArrayCreate (kCFAllocatorSystemDefault , NULL , 0 , & kCFTypeArrayCallBacks );
1311
+ }
1295
1312
if (pathCount > 0 )
1296
1313
{
1297
1314
size_t validPathCount = 0 ;
@@ -1304,16 +1321,25 @@ CF_PRIVATE CFArrayRef _CFTokenizeStringToCFArrayOfCFStrings(const char *values,
1304
1321
while (path )
1305
1322
{
1306
1323
assert (validPathCount < pathCount );
1307
- CFStringRef dirPath = CFStringCreateWithCString (kCFAllocatorSystemDefault , strdup (path ), _kCFXDGStringEncoding );
1324
+ char * pathString = strdup (path );
1325
+ CFStringRef dirPath = CFStringCreateWithCString (kCFAllocatorSystemDefault , pathString , _kCFXDGStringEncoding );
1308
1326
CFStringRef slash = CFSTR ("/" );
1309
1327
CFStringRef tilde = CFSTR ("~" );
1310
1328
// Check for absolutePath, if not ignore.
1311
1329
if (CFStringHasPrefix (dirPath , slash ) || CFStringHasPrefix (dirPath , tilde ) ) {
1312
1330
pathList [validPathCount ++ ] = dirPath ;
1331
+ } else {
1332
+ CFRelease (dirPath );
1313
1333
}
1314
- path = strtok (0 , delimiterStr );
1334
+ path = strtok (NULL , delimiterStr );
1335
+ free (pathString );
1336
+ }
1337
+ free (copyDirPath );
1338
+ CFArrayRef pathArray = CFArrayCreate (kCFAllocatorSystemDefault , (const void * * )pathList , validPathCount , & kCFTypeArrayCallBacks );
1339
+ for (int i = 0 ; i < validPathCount ; i ++ ) {
1340
+ CFRelease (pathList [i ]);
1315
1341
}
1316
- return CFArrayCreate ( kCFAllocatorSystemDefault , ( const void * * ) pathList , validPathCount , & kCFTypeArrayCallBacks ) ;
1342
+ return pathArray ;
1317
1343
}
1318
1344
return CFArrayCreate (kCFAllocatorSystemDefault , NULL , 0 , & kCFTypeArrayCallBacks );
1319
1345
}
0 commit comments