Skip to content

Commit db5c83a

Browse files
committed
Replace the variable std_path with the function std_path()
This commit also fixes a memory leak in onstdpath() through the unused value returned by defpath_init().
1 parent f4824e4 commit db5c83a

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/cmd/ksh93/sh/path.c

+18-16
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,23 @@ static_fn bool path_chkpaths(Shell_t *, Pathcomp_t *, Pathcomp_t *, Pathcomp_t *
7272
static_fn void path_checkdup(Shell_t *shp, Pathcomp_t *);
7373
static_fn Pathcomp_t *defpath_init(Shell_t *shp);
7474

75-
static const char *std_path = NULL;
75+
static_fn const char *std_path(void) {
76+
static const char *path = NULL;
77+
if (!path) {
78+
path = cs_path();
79+
if (path) {
80+
// Value returned by cs_path() is short lived, duplicate the string.
81+
// https://github.com/att/ast/issues/959
82+
path = strdup(path);
83+
} else {
84+
path = e_defpath;
85+
}
86+
}
87+
return path;
88+
}
7689

77-
static_fn bool onstdpath(Shell_t *shp, const char *name) {
78-
if (!std_path) defpath_init(shp);
79-
const char *cp = std_path, *sp;
90+
static_fn bool onstdpath(const char *name) {
91+
const char *cp = std_path(), *sp;
8092
if (cp) {
8193
while (*cp) {
8294
for (sp = name; *sp && (*cp == *sp); sp++, cp++) {
@@ -375,7 +387,7 @@ static_fn void path_checkdup(Shell_t *shp, Pathcomp_t *pp) {
375387
pp->mtime = statb.st_mtime;
376388
pp->ino = statb.st_ino;
377389
pp->dev = statb.st_dev;
378-
if (*name == '/' && onstdpath(shp, name)) flag = PATH_STD_DIR;
390+
if (*name == '/' && onstdpath(name)) flag = PATH_STD_DIR;
379391
first = (pp->flags & PATH_CDPATH) ? shp->cdpathlist : path_get(shp, "");
380392
for (oldpp = first; oldpp && oldpp != pp; oldpp = oldpp->next) {
381393
if (pp->ino == oldpp->ino && pp->dev == oldpp->dev && pp->mtime == oldpp->mtime) {
@@ -429,17 +441,7 @@ Pathcomp_t *path_nextcomp(Shell_t *shp, Pathcomp_t *pp, const char *name, Pathco
429441
}
430442

431443
static_fn Pathcomp_t *defpath_init(Shell_t *shp) {
432-
if (!std_path) {
433-
std_path = cs_path();
434-
if (std_path) {
435-
// Value returned by cs_path() is short lived, duplicate the string.
436-
// https://github.com/att/ast/issues/959
437-
std_path = strdup(std_path);
438-
} else {
439-
std_path = e_defpath;
440-
}
441-
}
442-
return path_addpath(shp, NULL, std_path, PATH_PATH);
444+
return path_addpath(shp, NULL, std_path(), PATH_PATH);
443445
}
444446

445447
static_fn void path_init(Shell_t *shp) {

0 commit comments

Comments
 (0)