tpl: Add missed mutex lock / unlock on __tpl_runtime_fini() 93/129593/2
authorjoonbum.ko <joonbum.ko@samsung.com>
Wed, 17 May 2017 06:25:58 +0000 (15:25 +0900)
committerjoonbum.ko <joonbum.ko@samsung.com>
Wed, 17 May 2017 06:56:28 +0000 (15:56 +0900)
 If race condition occures on __tpl_hashlist_destroy() of __tpl_runtime_fini(),
 then __tpl_hashlist_destroy() can raise double free.
 This patch fixes it.

Change-Id: Ib438e7e9b3a69d330fbda05d8090d76a80a19222
Signed-off-by: joonbum.ko <joonbum.ko@samsung.com>
src/tpl.c

index 23cf39b..afde8d8 100644 (file)
--- a/src/tpl.c
+++ b/src/tpl.c
@@ -2,7 +2,7 @@
 
 unsigned int tpl_log_lvl = 0;
 unsigned int tpl_log_initialized = 0;
-unsigned int tpl_dump_lvl;
+unsigned int tpl_dump_lvl = 0;
 
 struct _tpl_runtime {
        tpl_hlist_t *displays[TPL_BACKEND_COUNT];
@@ -40,9 +40,12 @@ __tpl_runtime_fini(void)
        if (runtime != NULL) {
                int i;
 
-               for (i = 0; i < TPL_BACKEND_COUNT; i++) {
-                       if (runtime->displays[i] != NULL)
-                               __tpl_hashlist_destroy(&(runtime->displays[i]));
+               if (!pthread_mutex_lock(&runtime_mutex)) {
+                       for (i = 0; i < TPL_BACKEND_COUNT; i++) {
+                               if (runtime->displays[i] != NULL)
+                                       __tpl_hashlist_destroy(&(runtime->displays[i]));
+                       }
+                       pthread_mutex_unlock(&runtime_mutex);
                }
 
                free(runtime);
@@ -71,7 +74,7 @@ __tpl_util_sys_yield(void)
 int
 __tpl_util_clz(int val)
 {
-       return __builtin_clz( val );
+       return __builtin_clz(val);
 }
 
 int
@@ -99,7 +102,7 @@ __tpl_util_atomic_set(tpl_util_atomic_uint *const atom, unsigned int val)
 }
 
 unsigned int
-__tpl_util_atomic_inc(tpl_util_atomic_uint *const atom )
+__tpl_util_atomic_inc(tpl_util_atomic_uint *const atom)
 {
        TPL_ASSERT(atom);
 
@@ -107,7 +110,7 @@ __tpl_util_atomic_inc(tpl_util_atomic_uint *const atom )
 }
 
 unsigned int
-__tpl_util_atomic_dec( tpl_util_atomic_uint *const atom )
+__tpl_util_atomic_dec(tpl_util_atomic_uint *const atom)
 {
        TPL_ASSERT(atom);