From 0d147af1c4ecfe24c46952e059a78ad1e6b28553 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Tue, 21 Oct 2014 00:54:28 +0400 Subject: [PATCH] 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). --- misc.c | 2 +- os_dep.c | 8 ++++---- pthread_support.c | 15 +++++++++------ tests/test.c | 12 +++++++++--- win32_threads.c | 2 +- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/misc.c b/misc.c index 1f60e8e..6c01404 100644 --- 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 */ diff --git a/os_dep.c b/os_dep.c index 4a77d31..1709c8e 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 @@ -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 diff --git a/pthread_support.c b/pthread_support.c index 33c8a33..2fca360 100644 --- a/pthread_support.c +++ b/pthread_support.c @@ -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 } diff --git a/tests/test.c b/tests/test.c index 6f412c8..1f006e4 100644 --- a/tests/test.c +++ b/tests/test.c @@ -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 (); diff --git a/win32_threads.c b/win32_threads.c index fc65743..31d33b6 100644 --- a/win32_threads.c +++ b/win32_threads.c @@ -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); } -- 2.7.4