Skip to content

Commit e342646

Browse files
authored
Ensure strsep operates on a copy of PATH. (#5289) (#5296)
strsep modifies its first argument, which means that if we don't make a copy, any PATH processing that may occur after _CFProcessPath executes breaks because strsep will effectively change PATH to be only the first list item. For example, swiftpm's tool finding fails because it searches PATH, and if the tool in question doesn't reside in the first PATH item, things break. This only occurs on BSD and only when the program name, i.e., argv[0], is not an absolute path.
1 parent 265274a commit e342646

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

Sources/CoreFoundation/CFPlatform.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,12 @@ const char *_CFProcessPath(void) {
266266
}
267267

268268
// Search PATH.
269-
if (argv0) {
270-
char *paths = getenv("PATH");
269+
char *path = getenv("PATH");
270+
if (argv0 && path) {
271+
char *paths = strdup(path);
272+
char *remaining = paths;
271273
char *p = NULL;
272-
while ((p = strsep(&paths, ":")) != NULL) {
274+
while ((p = strsep(&remaining, ":")) != NULL) {
273275
char pp[PATH_MAX];
274276
int l = snprintf(pp, PATH_MAX, "%s/%s", p, argv0);
275277
if (l >= PATH_MAX) {
@@ -283,11 +285,13 @@ const char *_CFProcessPath(void) {
283285
_CFSetProgramNameFromPath(res);
284286
free(argv0);
285287
free(res);
288+
free(paths);
286289
return __CFProcessPath;
287290
}
288291
free(res);
289292
}
290293
free(argv0);
294+
free(paths);
291295
}
292296

293297
// See if the shell will help.

0 commit comments

Comments
 (0)