Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nshlib/nsh_parse: Closing fds opened for redirection if necessary
Browse files Browse the repository at this point in the history
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]>
JianyuWang0623 committed Jan 16, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent c27607e commit c75dc2d
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions nshlib/nsh_parse.c
Original file line number Diff line number Diff line change
@@ -500,6 +500,8 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
int argc, FAR char *argv[],
FAR const struct nsh_param_s *param)
{
int fd_out = STDOUT_FILENO;
int fd_in = STDIN_FILENO;
int ret;

/* DO NOT CHANGE THE ORDERING OF THE FOLLOWING STEPS
@@ -645,9 +647,6 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
{
uint8_t save[SAVE_SIZE];

int fd_in = STDIN_FILENO;
int fd_out = STDOUT_FILENO;

/* Redirected output? */

if (vtbl->np.np_redir_out)
@@ -665,7 +664,8 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open",
NSH_ERRNO);
return nsh_saveresult(vtbl, true);
ret = errno;
goto close_redir;
}
}
else
@@ -691,7 +691,8 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open",
NSH_ERRNO);
return nsh_saveresult(vtbl, true);
ret = errno;
goto close_redir;
}
}
else
@@ -724,22 +725,27 @@ static int nsh_execute(FAR struct nsh_vtbl_s *vtbl,
{
nsh_undirect(vtbl, save);
}
}

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

if (ret < 0)
{
return nsh_saveresult(vtbl, true);
}
/* Closing fds opened for redirection if necessary */

if (fd_out > STDOUT_FILENO)
{
close(fd_out);
}

if (fd_in > STDIN_FILENO)
{
close(fd_in);
}

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

return nsh_saveresult(vtbl, false);
return nsh_saveresult(vtbl, ret != OK);
}

/****************************************************************************

0 comments on commit c75dc2d

Please sign in to comment.