[sane-devel] [PATCH] Do not accidentally close STDIN/STDOUT/STDERR

Nick Andrew nick at nick-andrew.net
Fri Aug 1 00:29:11 UTC 2008


Do not accidentally close STDIN/STDOUT/STDERR

When /dev/null is opened it gets the lowest numbered unopened
file descriptor. If any of the file descriptors 0, 1 or 2 are
closed when the program starts running, the later close()
will cause that file descriptor to be closed, which is not
wanted.

Add a test before the close(fd) call to ensure that file
descriptors 0, 1 or 2 are not closed by accident.

Signed-off-by: Nick Andrew <nick at nick-andrew.net>
---

 frontend/saned.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)


diff --git a/frontend/saned.c b/frontend/saned.c
index 020af79..47c6ded 100644
--- a/frontend/saned.c
+++ b/frontend/saned.c
@@ -2815,7 +2815,10 @@ run_standalone (int argc, char **argv)
       dup2 (fd, STDOUT_FILENO);
       dup2 (fd, STDERR_FILENO);
 
-      close (fd);
+      if (fd > STDERR_FILENO)
+        {
+          close (fd);
+	}
 
       setsid ();
 





More information about the sane-devel mailing list