Fix wrong implementations of vconf API 31/273731/2
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 13 Apr 2022 05:32:04 +0000 (14:32 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 13 Apr 2022 06:17:36 +0000 (15:17 +0900)
- Adds nullptr check before calling g_main_context_ref()
- Fixes wrong if statement about gsource_tbl
- Fixes wrong locking & unlocking mutex
- Changes the return value of the _vconf_idle_add_full() function to source_id

Change-Id: I4ca52354902958654fb149a9e6b64dd1e9a35325
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
vconf-compat/vconf.c

index 091dc62..66a9166 100644 (file)
@@ -84,7 +84,7 @@ struct callback_info {
        keynode_t *key_node;
        struct noti_cb *noticb;
        gint id;
-       GSource *source;
+       guint source_id;
 };
 
 static struct noti_cb *_vconf_find_noti_cb(struct noti *noti, vconf_callback_fn cb);
@@ -100,10 +100,11 @@ static void _vconf_mutex_unlock(void)
        g_rec_mutex_unlock(&vconf_mutex);
 }
 
-static GSource *_vconf_idle_add_full(GMainContext *context, guint priority, GSourceFunc func,
+static guint _vconf_idle_add_full(GMainContext *context, guint priority, GSourceFunc func,
                gpointer data)
 {
        GSource *source;
+       guint source_id;
 
        source = g_idle_source_new();
        if (!source)
@@ -111,10 +112,10 @@ static GSource *_vconf_idle_add_full(GMainContext *context, guint priority, GSou
 
        g_source_set_callback(source, func, data, NULL);
        g_source_set_priority(source, priority);
-       g_source_attach(source, context);
+       source_id = g_source_attach(source, context);
        g_source_unref(source);
 
-       return source;
+       return source_id;
 }
 
 static void _vconf_free_keynode(struct _keynode_t *keynode)
@@ -159,7 +160,9 @@ static struct noti_cb *_vconf_copy_noti_cb(struct noti_cb *noticb)
        clone->user_data = noticb->user_data;
        clone->active = noticb->active;
        clone->in_cb = noticb->in_cb;
-       clone->thread_ctx = g_main_context_ref(noticb->thread_ctx);
+
+       if (noticb->thread_ctx)
+               clone->thread_ctx = g_main_context_ref(noticb->thread_ctx);
 
        return clone;
 }
@@ -177,11 +180,19 @@ static void _vconf_free_noti_cb(struct noti_cb *noticb)
 
 static void _vconf_free_callback_info(struct callback_info *cb_info)
 {
+       GMainContext *context;
+       GSource *source;
+
        if (!cb_info)
                return;
 
-       if (cb_info->source && !g_source_is_destroyed(cb_info->source))
-               g_source_destroy(cb_info->source);
+       if (cb_info->source_id != 0 && cb_info->noticb) {
+               context = cb_info->noticb->thread_ctx;
+               source = g_main_context_find_source_by_id(context,
+                               cb_info->source_id);
+               if (source && !g_source_is_destroyed(source))
+                       g_source_destroy(source);
+       }
 
        _vconf_free_noti_cb(cb_info->noticb);
        _vconf_free_keynode(cb_info->key_node);
@@ -213,9 +224,9 @@ static struct callback_info *_vconf_create_callback_info(const char *key,
        g_atomic_int_inc(&id);
        cb_info->id = id;
 
-       cb_info->source = _vconf_idle_add_full(cb_info->noticb->thread_ctx, G_PRIORITY_HIGH,
-                       _vconf_call_noti_cb, cb_info);
-       if (!cb_info->source) {
+       cb_info->source_id = _vconf_idle_add_full(cb_info->noticb->thread_ctx,
+                       G_PRIORITY_HIGH, _vconf_call_noti_cb, cb_info);
+       if (cb_info->source_id == 0) {
                _vconf_free_callback_info(cb_info);
                return NULL;
        }
@@ -522,19 +533,19 @@ static gboolean _vconf_call_noti_cb(gpointer data)
        struct noti_cb *noticb = cb_info->noticb;
        struct noti *noti;
 
-       cb_info->source = NULL;
+       cb_info->source_id = 0;
 
        _vconf_mutex_lock();
        noti = g_hash_table_lookup(noti_tbl, node->keyname);
        if (!noti) {
-               _vconf_mutex_unlock();
                g_hash_table_remove(gsource_tbl, GINT_TO_POINTER(cb_info->id));
+               _vconf_mutex_unlock();
                return G_SOURCE_REMOVE;
        }
 
        if (!_vconf_find_noti_cb(noti, noticb->cb)) {
-               _vconf_mutex_unlock();
                g_hash_table_remove(gsource_tbl, GINT_TO_POINTER(cb_info->id));
+               _vconf_mutex_unlock();
                return G_SOURCE_REMOVE;
        }
        _vconf_mutex_unlock();
@@ -563,7 +574,7 @@ static void _vconf_notify_cb(const struct buxton_layer *layer, const char *key,
                return;
        }
 
-       if (gsource_tbl) {
+       if (gsource_tbl == NULL) {
                gsource_tbl = g_hash_table_new_full(g_direct_hash,
                                g_direct_equal, NULL,
                                (GDestroyNotify)_vconf_free_callback_info);