7729eb
commit 558168c78ea1eb8efb33959c1da9d6b5a997fd7b
7729eb
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
7729eb
Date:   Wed Oct 6 21:48:35 2021 +0530
7729eb
7729eb
    support: Also return fd when it is 0
7729eb
    
7729eb
    The fd validity check in open_dev_null checks if fd > 0, which would
7729eb
    lead to a leaked fd if it is == 0.
7729eb
    
7729eb
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
7729eb
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
7729eb
    (cherry picked from commit 27b6edbb090f736b101f569620d8ad0e7217ddf8)
7729eb
7729eb
diff --git a/support/support-open-dev-null-range.c b/support/support-open-dev-null-range.c
7729eb
index 80d9dba50402ce12..66a850410557351b 100644
7729eb
--- a/support/support-open-dev-null-range.c
7729eb
+++ b/support/support-open-dev-null-range.c
7729eb
@@ -40,16 +40,16 @@ increase_nofile (void)
7729eb
 static int
7729eb
 open_dev_null (int flags, mode_t mode)
7729eb
 {
7729eb
- int fd = open64 ("/dev/null", flags, mode);
7729eb
- if (fd > 0)
7729eb
-   return fd;
7729eb
+  int fd = open64 ("/dev/null", flags, mode);
7729eb
+  if (fd >= 0)
7729eb
+    return fd;
7729eb
 
7729eb
- if (fd < 0 && errno != EMFILE)
7729eb
-   FAIL_EXIT1 ("open64 (\"/dev/null\", 0x%x, 0%o): %m", flags, mode);
7729eb
+  if (fd < 0 && errno != EMFILE)
7729eb
+    FAIL_EXIT1 ("open64 (\"/dev/null\", 0x%x, 0%o): %m", flags, mode);
7729eb
 
7729eb
- increase_nofile ();
7729eb
+  increase_nofile ();
7729eb
 
7729eb
- return xopen ("/dev/null", flags, mode);
7729eb
+  return xopen ("/dev/null", flags, mode);
7729eb
 }
7729eb
 
7729eb
 struct range