Skip to content

Commit

Permalink
Close file descriptor after dup2
Browse files Browse the repository at this point in the history
Close the file descriptor assgined by open if it
is not the same as the newfd.

Fixes coverity 1028694

(It seems likely that coverity will not agree that it is
fixed, since it will not recognize the belt and suspenders
checking of the fd being the value dup'ed to).
  • Loading branch information
garybuhrmaster committed Jun 17, 2013
1 parent b551172 commit 959a807
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions mythtv/libs/libmythbase/mythsystemunix.cpp
Expand Up @@ -920,6 +920,15 @@ void MythSystemLegacyUnix::Fork(time_t timeout)
"\n\t\t\tfailed to duplicate file descriptor: "
<< strerror(errno) << endl;
}
if (fd != 0) // if fd was zero, do not close
{
if (close(fd) < 0)
{
cerr << locerr
<< "Unable to close stdin redirect /dev/null: "
<< strerror(errno) << endl;
}
}
}
else
{
Expand Down Expand Up @@ -955,6 +964,15 @@ void MythSystemLegacyUnix::Fork(time_t timeout)
"\n\t\t\tfailed to duplicate file descriptor: "
<< strerror(errno) << endl;
}
if (fd != 1) // if fd was one, do not close
{
if (close(fd) < 0)
{
cerr << locerr
<< "Unable to close stdout redirect /dev/null: "
<< strerror(errno) << endl;
}
}
}
else
{
Expand Down Expand Up @@ -990,6 +1008,15 @@ void MythSystemLegacyUnix::Fork(time_t timeout)
"\n\t\t\tfailed to duplicate file descriptor: "
<< strerror(errno) << endl;
}
if (fd != 2) // if fd was two, do not close
{
if (close(fd) < 0)
{
cerr << locerr
<< "Unable to close stderr redirect /dev/null: "
<< strerror(errno) << endl;
}
}
}
else
{
Expand Down

0 comments on commit 959a807

Please sign in to comment.