@@ -696,7 +696,7 @@ static_fn bool pwdinfpath(void) {
696
696
//
697
697
Pathcomp_t * path_absolute (Shell_t * shp , const char * name , Pathcomp_t * pp ) {
698
698
int isfun ;
699
- int f = -1 ;
699
+ int fd = -1 ;
700
700
int noexec = 0 ;
701
701
Pathcomp_t * oldpp ;
702
702
Namval_t * np ;
@@ -813,18 +813,21 @@ Pathcomp_t *path_absolute(Shell_t *shp, const char *name, Pathcomp_t *pp) {
813
813
}
814
814
shp -> bltin_dir = NULL ;
815
815
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 , '.' ))) {
818
818
* 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
+ }
820
823
* cp = '.' ;
821
824
}
822
- if (isfun && f >= 0 ) {
825
+ if (isfun && fd >= 0 ) {
823
826
nv_onattr (nv_open (name , sh_subfuntree (shp , 1 ), NV_NOARRAY | NV_IDENT | NV_NOSCOPE ),
824
827
NV_LTOU | NV_FUNCTION );
825
- funload (shp , f , name );
828
+ funload (shp , fd , name );
826
829
return NULL ;
827
- } else if (f >= 0 && (oldpp -> flags & PATH_STD_DIR )) {
830
+ } else if (fd >= 0 && (oldpp -> flags & PATH_STD_DIR )) {
828
831
int n = stktell (shp -> stk );
829
832
sfputr (shp -> stk , "/bin/" , -1 );
830
833
sfputr (shp -> stk , name , 0 );
@@ -837,16 +840,23 @@ Pathcomp_t *path_absolute(Shell_t *shp, const char *name, Pathcomp_t *pp) {
837
840
nv_setattr (np , nvflags );
838
841
}
839
842
}
840
- if (!pp || f >= 0 ) break ;
843
+ if (!pp || fd >= 0 ) break ;
841
844
if (errno != ENOENT ) noexec = errno ;
842
845
// 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
+ }
844
851
}
845
852
846
- if (f < 0 ) {
853
+ if (fd == -1 ) {
847
854
shp -> path_err = (noexec ? noexec : ENOENT );
848
855
return NULL ;
849
856
}
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 );
850
860
sfputc (shp -> stk , 0 );
851
861
return oldpp ;
852
862
}
0 commit comments