Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

Commit 58bc8b5

Browse files
committed
Coverity CID#287601 Resource leak
This should fix another theoretical file descriptor leak. As with the previous change it is unclear this can actually happen. But better safe than sorry and this should eliminate a lint warning.
1 parent 31f76f6 commit 58bc8b5

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/cmd/ksh93/sh/path.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ static_fn bool pwdinfpath(void) {
696696
//
697697
Pathcomp_t *path_absolute(Shell_t *shp, const char *name, Pathcomp_t *pp) {
698698
int isfun;
699-
int f = -1;
699+
int fd = -1;
700700
int noexec = 0;
701701
Pathcomp_t *oldpp;
702702
Namval_t *np;
@@ -813,18 +813,21 @@ Pathcomp_t *path_absolute(Shell_t *shp, const char *name, Pathcomp_t *pp) {
813813
}
814814
shp->bltin_dir = NULL;
815815
sh_stats(STAT_PATHS);
816-
f = can_execute(shp, stkptr(shp->stk, PATH_OFFSET), isfun);
817-
if (isfun && f >= 0 && (cp = strrchr(name, '.'))) {
816+
fd = can_execute(shp, stkptr(shp->stk, PATH_OFFSET), isfun);
817+
if (isfun && fd >= 0 && (cp = strrchr(name, '.'))) {
818818
*cp = 0;
819-
if (nv_open(name, sh_subfuntree(shp, 1), NV_NOARRAY | NV_IDENT | NV_NOSCOPE)) f = -1;
819+
if (nv_open(name, sh_subfuntree(shp, 1), NV_NOARRAY | NV_IDENT | NV_NOSCOPE)) {
820+
sh_close(fd);
821+
fd = -1;
822+
}
820823
*cp = '.';
821824
}
822-
if (isfun && f >= 0) {
825+
if (isfun && fd >= 0) {
823826
nv_onattr(nv_open(name, sh_subfuntree(shp, 1), NV_NOARRAY | NV_IDENT | NV_NOSCOPE),
824827
NV_LTOU | NV_FUNCTION);
825-
funload(shp, f, name);
828+
funload(shp, fd, name);
826829
return NULL;
827-
} else if (f >= 0 && (oldpp->flags & PATH_STD_DIR)) {
830+
} else if (fd >= 0 && (oldpp->flags & PATH_STD_DIR)) {
828831
int n = stktell(shp->stk);
829832
sfputr(shp->stk, "/bin/", -1);
830833
sfputr(shp->stk, name, 0);
@@ -837,16 +840,23 @@ Pathcomp_t *path_absolute(Shell_t *shp, const char *name, Pathcomp_t *pp) {
837840
nv_setattr(np, nvflags);
838841
}
839842
}
840-
if (!pp || f >= 0) break;
843+
if (!pp || fd >= 0) break;
841844
if (errno != ENOENT) noexec = errno;
842845
// It's not clear this can actually happen but Coverity Scan says it is possible.
843-
if (f != -1) sh_close(f);
846+
// No unit test causes this condition to be true.
847+
if (fd != -1) {
848+
sh_close(fd);
849+
fd = -1;
850+
}
844851
}
845852

846-
if (f < 0) {
853+
if (fd == -1) {
847854
shp->path_err = (noexec ? noexec : ENOENT);
848855
return NULL;
849856
}
857+
858+
// If we reach this point fd must be stdin (i.e., zero) since we intend to read the file.
859+
assert(fd == 0);
850860
sfputc(shp->stk, 0);
851861
return oldpp;
852862
}

src/cmd/ksh93/tests/types.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,9 @@ unset z
574574
u_t -a x | read z
575575
[[ $z == unset ]] && log_error 'unset discipline called on type creation'
576576
577-
{ z=$($SHELL 2> /dev/null 'typeset -T foo; typeset -T') ;} 2> /dev/null
578-
[[ $z == 'typeset -T foo' ]] || log_error '"typeset -T foo; typeset -T" failed'
577+
expect='typeset -T foo'
578+
{ actual=$($SHELL 'typeset -T foo; typeset -T') ;}
579+
[[ $actual == $expect ]] || log_error '"typeset -T foo; typeset -T" failed' "$expect" "$actual"
579580
580581
{ z=$($SHELL 2> /dev/null 'typeset -T foo=bar; typeset -T') ;} 2> /dev/null
581582
[[ $z ]] && log_error '"typeset -T foo=bar" should not creates type foo'

0 commit comments

Comments
 (0)