test/test-pthread.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) New commits: commit 6e414d61c77b2e6c08d0694ae61b848ee836b7eb Author: Ben Wagner <bungeman@xxxxxxxxxxxx> Date: Tue Aug 31 13:03:25 2021 -0400 Extend test thread args lifetime The argument passed to each thread in test-pthread.c indicates a thread number to report when finished. This value is read out by the thread into a local variable early in the thread's execution. Currently, the address passed as this argument is the address of a loop local. However, it is possible that the created thread will not be scheduled to run or will not read the argument before the thread creation loop finishes and the local is destroyed. This can lead to odd behavior, usually observed as multiple threads reporting the same thread_num. Fix this issue by storing the thread arguments in a parallel array to the array of threads. This ensures that the thread arguments are in scope as long as the threads themselves. Discovered with tests/test-pthread with AddressSanitizer enabled. diff --git a/test/test-pthread.c b/test/test-pthread.c index fbf397d..c15c876 100644 --- a/test/test-pthread.c +++ b/test/test-pthread.c @@ -71,17 +71,17 @@ static void *run_test_in_thread(void *arg) int main(int argc,char **argv) { pthread_t threads[NTHR]; + struct thr_arg_s thr_args[NTHR]; int i, j; printf("Creating %d threads\n",NTHR); for(i = 0;i<NTHR;i++) { - struct thr_arg_s thr_arg; int result; - thr_arg.thr_num=i; + thr_args[i].thr_num=i; result = pthread_create(&threads[i],NULL,run_test_in_thread, - (void *)&thr_arg); + (void *)&thr_args[i]); if(result!=0) { fprintf(stderr,"Cannot create thread %d\n",i);