From 2fb79a63263f5811770c783c91ff98d317dab528 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Sat, 14 Dec 2019 23:58:45 +0100 Subject: [PATCH] Fix some races protect some more accesses to global data with the semaphore. (And for the testcase: don't just write into global data, use local copies; it's not important for speed here). --- libtcc.c | 4 ++++ tests/libtcc_test_mt.c | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/libtcc.c b/libtcc.c index 3094d2df..e1bfe028 100644 --- a/libtcc.c +++ b/libtcc.c @@ -749,7 +749,9 @@ LIBTCCAPI TCCState *tcc_new(void) s = tcc_mallocz(sizeof(TCCState)); if (!s) return NULL; + WAIT_SEM(); ++nb_states; + POST_SEM(); #undef gnu_ext @@ -958,8 +960,10 @@ LIBTCCAPI void tcc_delete(TCCState *s1) #endif tcc_free(s1); + WAIT_SEM(); if (0 == --nb_states) tcc_memcheck(); + POST_SEM(); } LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) diff --git a/tests/libtcc_test_mt.c b/tests/libtcc_test_mt.c index 266870fe..03b9f799 100644 --- a/tests/libtcc_test_mt.c +++ b/tests/libtcc_test_mt.c @@ -105,13 +105,15 @@ void parse_args(TCCState *s) else if (a[1] == 'L') tcc_add_library_path(s, a+2); else if (a[1] == 'D') { - char *eq = strchr(a+2, '='); + char *dup = strdup(a); + char *eq = strchr(dup+2, '='); if (eq) { *eq = 0; - tcc_define_symbol(s, a+2, eq+1); + tcc_define_symbol(s, dup+2, eq+1); *eq = '='; } else - tcc_define_symbol(s, a+2, NULL); + tcc_define_symbol(s, dup+2, NULL); + free(dup); } } }