]> granicus.if.org Git - gc/commitdiff
Fix unchecked fork() result in gctest (Unix, Cygwin)
authorIvan Maidanski <ivmai@mail.ru>
Fri, 24 Jun 2016 20:01:53 +0000 (23:01 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Fri, 24 Jun 2016 20:01:53 +0000 (23:01 +0300)
Fail gctest if forked process failed.

* tests/test.c [!NO_TEST_HANDLE_FORK]: Include sys/types.h, sys/wait.h.
* tests/test.c (run_one_test) [!NO_TEST_HANDLE_FORK]: Declare pid,
wstatus local variables; FAIL if fork() returns -1; call waitpid() for
the child process; FAIL if the child process failed.

tests/test.c

index 9a588b27f122f94bd272c0208a65a3d0543ed3e7..4e0280af1d785ef53086d35a4410899ba28a85de 100644 (file)
@@ -96,6 +96,8 @@
 
 # ifndef NO_TEST_HANDLE_FORK
 #   include <unistd.h>
+#   include <sys/types.h>
+#   include <sys/wait.h>
 #   ifdef HANDLE_FORK
 #     define INIT_FORK_SUPPORT GC_set_handle_fork(1)
                 /* Causes abort in GC_init on pthread_atfork failure.   */
@@ -1203,6 +1205,10 @@ void run_one_test(void)
     CLOCK_TYPE reverse_time;
     CLOCK_TYPE tree_time;
     unsigned long time_diff;
+#   ifndef NO_TEST_HANDLE_FORK
+      pid_t pid;
+      int wstatus;
+#   endif
 
 #   ifdef FIND_LEAK
         GC_printf(
@@ -1366,10 +1372,23 @@ void run_one_test(void)
         GC_free(GC_malloc_atomic(0));
 #   ifndef NO_TEST_HANDLE_FORK
         GC_atfork_prepare();
-        if (fork() != 0) {
+        pid = fork();
+        if (pid != 0) {
           GC_atfork_parent();
+          if (pid == -1) {
+            GC_printf("Process fork failed\n");
+            FAIL;
+          }
           if (print_stats)
-            GC_log_printf("Forked child process (or failed)\n");
+            GC_log_printf("Forked child process\n");
+          if (waitpid(pid, &wstatus, 0) == -1) {
+            GC_printf("Wait for child process failed\n");
+            FAIL;
+          }
+          if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) {
+            GC_printf("Child process failed, status= 0x%x\n", wstatus);
+            FAIL;
+          }
         } else {
           GC_atfork_child();
           if (print_stats)