]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
timeout: defensive handling of all wait() errors
authorPádraig Brady <P@draigBrady.com>
Mon, 31 Aug 2009 18:18:27 +0000 (19:18 +0100)
committerPádraig Brady <P@draigBrady.com>
Tue, 1 Sep 2009 10:02:18 +0000 (11:02 +0100)
* src/timeout.c (main): Handle all possible cases of unexpected
failures from wait().  This was prompted by the clang tool reporting
the possible non-initialization of the status variable.

src/timeout.c

index 20efdddc0af2eb42825b1d3a57a9110ee35dc38f..62f3d4b6e24abe53064b41183ff3eb5ac84e9569 100644 (file)
@@ -317,12 +317,25 @@ main (int argc, char **argv)
          child exits, not on this process receiving a signal. Also we're not
          passing WUNTRACED | WCONTINUED to a waitpid() call and so will not get
          indication that the child has stopped or continued.  */
-      wait (&status);
-
-      if (WIFEXITED (status))
-        status = WEXITSTATUS (status);
-      else if (WIFSIGNALED (status))
-        status = WTERMSIG (status) + 128;     /* what sh does at least.  */
+      if (wait (&status) == -1)
+        {
+          /* shouldn't happen.  */
+          error (0, errno, _("error waiting for command"));
+          status = EXIT_CANCELED;
+        }
+      else
+        {
+          if (WIFEXITED (status))
+            status = WEXITSTATUS (status);
+          else if (WIFSIGNALED (status))
+            status = WTERMSIG (status) + 128; /* what sh does at least.  */
+          else
+            {
+              /* shouldn't happen.  */
+              error (0, 0, _("unknown status from command (0x%X)"), status);
+              status = EXIT_FAILURE;
+            }
+        }
 
       if (timed_out)
         return EXIT_TIMEDOUT;