From: Ivan Maidanski Date: Mon, 20 Oct 2014 20:54:28 +0000 (+0400) Subject: Fix missing error handling of pthread_attr_init/getstacksize X-Git-Tag: gc7_4_4~96 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0d3bd3db08249157eef52332ed8dbeb9678c94d6;p=gc Fix missing error handling of pthread_attr_init/getstacksize * 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). --- diff --git a/misc.c b/misc.c index 2fcba348..a5d7d28c 100644 --- a/misc.c +++ b/misc.c @@ -860,7 +860,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 */ diff --git a/os_dep.c b/os_dep.c index e11af95f..d3f1cd8c 100644 --- 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 @@ -4190,7 +4190,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 diff --git a/pthread_support.c b/pthread_support.c index 70958a32..883a5690 100644 --- a/pthread_support.c +++ b/pthread_support.c @@ -443,7 +443,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); } @@ -1702,14 +1702,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. */ @@ -2028,7 +2031,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 } diff --git a/tests/test.c b/tests/test.c index 34c6307b..8d0acb4c 100644 --- a/tests/test.c +++ b/tests/test.c @@ -1852,11 +1852,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) \ @@ -1894,7 +1900,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 (); diff --git a/win32_threads.c b/win32_threads.c index 200395c2..bfaca358 100644 --- a/win32_threads.c +++ b/win32_threads.c @@ -1767,7 +1767,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); }