Fix missing error handling of pthread_attr_init/getstacksize
authorIvan Maidanski <ivmai@mail.ru>
Mon, 20 Oct 2014 20:54:28 +0000 (00:54 +0400)
committerIvan Maidanski <ivmai@mail.ru>
Mon, 20 Oct 2014 20:58:36 +0000 (00:58 +0400)
* misc.c (GC_init): Explicitly ignore returned value of
pthread_mutexattr_destroy.
* os_dep.c (GC_get_main_stack_base, GC_get_stack_base, GC_dirty_init):
Likewise.
* pthread_support.c (start_mark_threads, pthread_create): Likewise.
* tests/test.c (main): Likewise.
* win32_threads.c (start_mark_threads): Likewise.
* pthread_support.c (pthread_create): ABORT (with the appropriate
message) in case of pthread_attr_getstacksize or pthread_attr_init
failure.
* tests/test.c (main): Print error code and FAIL if pthread_attr_init
or pthread_attr_setstacksize failed (only if GC_PTHREADS).

misc.c
os_dep.c
pthread_support.c
tests/test.c
win32_threads.c

diff --git a/misc.c b/misc.c
index 1f60e8e..6c01404 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -873,7 +873,7 @@ GC_API void GC_CALL GC_init(void)
           if (0 != pthread_mutex_init(&GC_allocate_ml, &mattr)) {
             ABORT("pthread_mutex_init failed");
           }
-          pthread_mutexattr_destroy(&mattr);
+          (void)pthread_mutexattr_destroy(&mattr);
         }
 #     endif
 #   endif /* THREADS */
index 4a77d31..1709c8e 100644 (file)
--- a/os_dep.c
+++ b/os_dep.c
@@ -1201,13 +1201,13 @@ GC_INNER word GC_page_size = 0;
       if (pthread_getattr_np(pthread_self(), &attr) == 0) {
         if (pthread_attr_getstack(&attr, &stackaddr, &size) == 0
             && stackaddr != NULL) {
-          pthread_attr_destroy(&attr);
+          (void)pthread_attr_destroy(&attr);
 #         ifdef STACK_GROWS_DOWN
             stackaddr = (char *)stackaddr + size;
 #         endif
           return (ptr_t)stackaddr;
         }
-        pthread_attr_destroy(&attr);
+        (void)pthread_attr_destroy(&attr);
       }
       WARN("pthread_getattr_np or pthread_attr_getstack failed"
            " for main thread\n", 0);
@@ -1284,7 +1284,7 @@ GC_INNER word GC_page_size = 0;
     if (pthread_attr_getstack(&attr, &(b -> mem_base), &size) != 0) {
         ABORT("pthread_attr_getstack failed");
     }
-    pthread_attr_destroy(&attr);
+    (void)pthread_attr_destroy(&attr);
 #   ifdef STACK_GROWS_DOWN
         b -> mem_base = (char *)(b -> mem_base) + size;
 #   endif
@@ -4186,7 +4186,7 @@ GC_INNER void GC_dirty_init(void)
   /* This will call the real pthread function, not our wrapper */
   if (pthread_create(&thread, &attr, GC_mprotect_thread, NULL) != 0)
     ABORT("pthread_create failed");
-  pthread_attr_destroy(&attr);
+  (void)pthread_attr_destroy(&attr);
 
   /* Setup the sigbus handler for ignoring the meaningless SIGBUSs */
 # ifdef BROKEN_EXCEPTION_HANDLING
index 33c8a33..2fca360 100644 (file)
@@ -445,7 +445,7 @@ start_mark_threads(void)
       }
     }
     GC_markers_m1 = i;
-    pthread_attr_destroy(&attr);
+    (void)pthread_attr_destroy(&attr);
     GC_COND_LOG_PRINTF("Started %d mark helper threads\n", GC_markers_m1);
 }
 
@@ -1706,14 +1706,17 @@ GC_API int WRAP_FUNC(pthread_create)(pthread_t *new_thread,
       {
         size_t stack_size = 0;
         if (NULL != attr) {
-           pthread_attr_getstacksize(attr, &stack_size);
+          if (pthread_attr_getstacksize(attr, &stack_size) != 0)
+            ABORT("pthread_attr_getstacksize failed");
         }
         if (0 == stack_size) {
            pthread_attr_t my_attr;
 
-           pthread_attr_init(&my_attr);
-           pthread_attr_getstacksize(&my_attr, &stack_size);
-           pthread_attr_destroy(&my_attr);
+           if (pthread_attr_init(&my_attr) != 0)
+             ABORT("pthread_attr_init failed");
+           if (pthread_attr_getstacksize(&my_attr, &stack_size) != 0)
+             ABORT("pthread_attr_getstacksize failed");
+           (void)pthread_attr_destroy(&my_attr);
         }
         /* On Solaris 10, with default attr initialization,     */
         /* stack_size remains 0.  Fudge it.                     */
@@ -2034,7 +2037,7 @@ static void setup_mark_lock(void)
       if (0 != pthread_mutex_init(&mark_mutex, &mattr)) {
         ABORT("pthread_mutex_init failed");
       }
-      pthread_mutexattr_destroy(&mattr);
+      (void)pthread_mutexattr_destroy(&mattr);
     }
 # endif
 }
index 6f412c8..1f006e4 100644 (file)
@@ -1874,11 +1874,17 @@ int main(void)
 #   endif
     GC_COND_INIT();
 
-    pthread_attr_init(&attr);
+    if ((code = pthread_attr_init(&attr)) != 0) {
+      GC_printf("pthread_attr_init failed, error=%d\n", code);
+      FAIL;
+    }
 #   if defined(GC_IRIX_THREADS) || defined(GC_FREEBSD_THREADS) \
         || defined(GC_DARWIN_THREADS) || defined(GC_AIX_THREADS) \
         || defined(GC_OPENBSD_THREADS)
-        pthread_attr_setstacksize(&attr, 1000000);
+        if ((code = pthread_attr_setstacksize(&attr, 1000000)) != 0) {
+          GC_printf("pthread_attr_setstacksize failed, error=%d\n", code);
+          FAIL;
+        }
 #   endif
     n_tests = 0;
 #   if (defined(MPROTECT_VDB)) && !defined(REDIRECT_MALLOC) \
@@ -1916,7 +1922,7 @@ int main(void)
     }
     check_heap_stats();
     (void)fflush(stdout);
-    pthread_attr_destroy(&attr);
+    (void)pthread_attr_destroy(&attr);
 #   ifdef PTW32_STATIC_LIB
         pthread_win32_thread_detach_np ();
         pthread_win32_process_detach_np ();
index fc65743..31d33b6 100644 (file)
@@ -1776,7 +1776,7 @@ GC_INNER void GC_get_next_stack(char *start, char *limit,
         }
       }
       GC_markers_m1 = i;
-      pthread_attr_destroy(&attr);
+      (void)pthread_attr_destroy(&attr);
       GC_COND_LOG_PRINTF("Started %d mark helper threads\n", GC_markers_m1);
     }