After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 730189 - gtestutils: Fix a very unlikely FD leak in test fork handling
gtestutils: Fix a very unlikely FD leak in test fork handling
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2014-05-15 11:47 UTC by Philip Withnall
Modified: 2014-05-26 07:40 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
gtestutils: Fix a very unlikely FD leak in test fork handling (1.27 KB, patch)
2014-05-15 11:47 UTC, Philip Withnall
rejected Details | Review
gtestutils: Error out if /dev/null stdin redirection fails (1.27 KB, patch)
2014-05-16 22:11 UTC, Philip Withnall
committed Details | Review

Description Philip Withnall 2014-05-15 11:47:22 UTC
Patch attached.
Comment 1 Philip Withnall 2014-05-15 11:47:24 UTC
Created attachment 276598 [details] [review]
gtestutils: Fix a very unlikely FD leak in test fork handling

Somehow if g_open("/dev/null") returns an FD < 3, it would previously
have been leaked.

Coverity issue: #1159482
Comment 2 Colin Walters 2014-05-15 12:43:02 UTC
Review of attachment 276598 [details] [review]:

Looks right.
Comment 3 Dan Winship 2014-05-15 12:49:36 UTC
Comment on attachment 276598 [details] [review]
gtestutils: Fix a very unlikely FD leak in test fork handling

>       if (!(test_trap_flags & G_TEST_TRAP_INHERIT_STDIN))
>         fd0 = g_open ("/dev/null", O_RDONLY, 0);
>-      if (sane_dup2 (stdout_pipe[1], 1) < 0 || sane_dup2 (stderr_pipe[1], 2) < 0 || (fd0 >= 0 && sane_dup2 (fd0, 0) < 0))
>+      if (sane_dup2 (stdout_pipe[1], 1) < 0 || sane_dup2 (stderr_pipe[1], 2) < 0 || (fd0 > -1 && sane_dup2 (fd0, 0) < 0))

It probably should g_error() out if g_open() returns -1

>         g_error ("failed to dup2() in forked test program: %s", g_strerror (errno));
>-      if (fd0 >= 3)
>+      if (fd0 > -1)
>         close (fd0);

This is wrong; if fd0 had been 0, 1, or, 2, then it already would have been closed at this point by one of the preceding dup2() calls. (And if it was 1 or 2, then the following dup2 call would have failed, causing us to hit the g_error().)
Comment 4 Philip Withnall 2014-05-16 22:11:52 UTC
Created attachment 276692 [details] [review]
gtestutils: Error out if /dev/null stdin redirection fails
Comment 5 Philip Withnall 2014-05-16 22:13:26 UTC
Comment on attachment 276598 [details] [review]
gtestutils: Fix a very unlikely FD leak in test fork handling

(In reply to comment #3)
> This is wrong; if fd0 had been 0, 1, or, 2, then it already would have been
> closed at this point by one of the preceding dup2() calls. (And if it was 1 or
> 2, then the following dup2 call would have failed, causing us to hit the
> g_error().)

ooh, that's subtle. Looks like the Coverity issue was a false positive then.

The new patch just adds a g_error().
Comment 6 Allison Karlitskaya (desrt) 2014-05-25 09:37:23 UTC
Review of attachment 276692 [details] [review]:

::: glib/gtestutils.c
@@ +2686,3 @@
+            g_error ("failed to open /dev/null for stdin redirection");
+        }
+      if (sane_dup2 (stdout_pipe[1], 1) < 0 || sane_dup2 (stderr_pipe[1], 2) < 0 || (fd0 > -1 && sane_dup2 (fd0, 0) < 0))

Can you drop the spurious change here?
Comment 7 Philip Withnall 2014-05-26 07:40:40 UTC
Pushed with that change removed.

Attachment 276692 [details] pushed as d3fd88d - gtestutils: Error out if /dev/null stdin redirection fails