Skip to content

Commit f1212a6

Browse files
JianyuWang0623xiaoxiang781216
authored andcommitted
nshlib/nsh_parse: Closing fds opened for redirection if necessary
Coverity Log CID 1612743: (#1 of 1): Resource leak (RESOURCE_LEAK) 12. leaked_handle: The handle variable fd_out goes out of scope and leaks the handle. Signed-off-by: wangjianyu3 <[email protected]>
1 parent adb4c3b commit f1212a6

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

nshlib/nsh_parse.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,8 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
500500
int argc, FAR char *argv[],
501501
FAR const struct nsh_param_s *param)
502502
{
503+
int fd_out = STDOUT_FILENO;
504+
int fd_in = STDIN_FILENO;
503505
int ret;
504506

505507
/* DO NOT CHANGE THE ORDERING OF THE FOLLOWING STEPS
@@ -635,9 +637,6 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
635637
{
636638
uint8_t save[SAVE_SIZE];
637639

638-
int fd_in = STDIN_FILENO;
639-
int fd_out = STDOUT_FILENO;
640-
641640
/* Redirected output? */
642641

643642
if (vtbl->np.np_redir_out)
@@ -655,7 +654,8 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
655654
{
656655
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open",
657656
NSH_ERRNO);
658-
return nsh_saveresult(vtbl, true);
657+
ret = errno;
658+
goto close_redir;
659659
}
660660
}
661661
else
@@ -681,7 +681,8 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
681681
{
682682
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open",
683683
NSH_ERRNO);
684-
return nsh_saveresult(vtbl, true);
684+
ret = errno;
685+
goto close_redir;
685686
}
686687
}
687688
else
@@ -714,22 +715,27 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
714715
{
715716
nsh_undirect(vtbl, save);
716717
}
718+
}
717719

718-
/* Mark errors so that it is possible to test for non-zero return
719-
* values in nsh scripts.
720-
*/
720+
close_redir:
721721

722-
if (ret < 0)
723-
{
724-
return nsh_saveresult(vtbl, true);
725-
}
722+
/* Closing fds opened for redirection if necessary */
723+
724+
if (fd_out > STDOUT_FILENO)
725+
{
726+
close(fd_out);
727+
}
728+
729+
if (fd_in > STDIN_FILENO)
730+
{
731+
close(fd_in);
726732
}
727733

728734
/* Return success if the command succeeded (or at least, starting of the
729735
* command task succeeded).
730736
*/
731737

732-
return nsh_saveresult(vtbl, false);
738+
return nsh_saveresult(vtbl, ret != OK);
733739
}
734740

735741
/****************************************************************************

0 commit comments

Comments
 (0)