Change DD_LIST_FOREACH to DD_LIST_FOREACH_SAFE to protect crash 68/208268/1 accepted/tizen_5.0_unified accepted/tizen/5.0/unified/20190621.094636 submit/tizen_5.0/20190621.025949
authorlokilee73 <changjoo.lee@samsung.com>
Fri, 21 Jun 2019 02:36:45 +0000 (11:36 +0900)
committerlokilee73 <changjoo.lee@samsung.com>
Fri, 21 Jun 2019 02:37:40 +0000 (11:37 +0900)
While running DD_LIST_FOREACH, one item in the list can be removed.
At that time, removed item can be refered in DD_LIST_FOREACH.
And it can cause crash.

Change-Id: I6c789e45c674583eea631eda22c4ff1ca6473ca5
Signed-off-by: lokilee73 <changjoo.lee@samsung.com>
src/callback.c
src/haptic.c

index 55ee461..eb54bc5 100644 (file)
@@ -44,13 +44,13 @@ static void battery_capacity_cb(keynode_t *key, void *data)
 {
        static device_callback_e type = DEVICE_CALLBACK_BATTERY_CAPACITY;
        struct device_cb_info *cb_info;
-       dd_list *elem;
+       dd_list *elem, *elem_next;
        int val;
 
        val = vconf_keynode_get_int(key);
 
        /* invoke the each callback with value */
-       DD_LIST_FOREACH(device_cb_list[type], elem, cb_info)
+       DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
                cb_info->cb(type, (void*)val, cb_info->data);
 }
 //LCOV_EXCL_STOP
@@ -60,13 +60,13 @@ static void battery_charging_cb(keynode_t *key, void *data)
 {
        static device_callback_e type = DEVICE_CALLBACK_BATTERY_CHARGING;
        struct device_cb_info *cb_info;
-       dd_list *elem;
+       dd_list *elem, *elem_next;
        int val;
 
        val = vconf_keynode_get_int(key);
 
        /* invoke the each callback with value */
-       DD_LIST_FOREACH(device_cb_list[type], elem, cb_info)
+       DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
                cb_info->cb(type, (void*)val, cb_info->data);
 }
 //LCOV_EXCL_STOP
@@ -76,7 +76,7 @@ static void battery_level_cb(keynode_t *key, void *data)
 {
        static device_callback_e type = DEVICE_CALLBACK_BATTERY_LEVEL;
        struct device_cb_info *cb_info;
-       dd_list *elem;
+       dd_list *elem, *elem_next;
        int val, status;
 
        val = vconf_keynode_get_int(key);
@@ -95,7 +95,7 @@ static void battery_level_cb(keynode_t *key, void *data)
                status = -1;
 
        /* invoke the each callback with value */
-       DD_LIST_FOREACH(device_cb_list[type], elem, cb_info)
+       DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
                cb_info->cb(type, (void*)status, cb_info->data);
 }
 //LCOV_EXCL_STOP
@@ -105,7 +105,7 @@ static void display_changed_cb(keynode_t *key, void *data)
 {
        static device_callback_e type = DEVICE_CALLBACK_DISPLAY_STATE;
        struct device_cb_info *cb_info;
-       dd_list *elem;
+       dd_list *elem, *elem_next;
        display_state_e state;
        int val;
 
@@ -123,7 +123,7 @@ static void display_changed_cb(keynode_t *key, void *data)
        }
 
        /* invoke the each callback with value */
-       DD_LIST_FOREACH(device_cb_list[type], elem, cb_info)
+       DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
                cb_info->cb(type, (void*)state, cb_info->data);
 }
 //LCOV_EXCL_STOP
@@ -139,7 +139,7 @@ static void flash_state_cb(GDBusConnection *conn,
 {
        static int type = DEVICE_CALLBACK_FLASH_BRIGHTNESS;
        struct device_cb_info *cb_info;
-       dd_list *elem;
+       dd_list *elem, *elem_next;
        int val;
 
        if (strncmp(signal, SIGNAL_FLASH_STATE,
@@ -153,7 +153,7 @@ static void flash_state_cb(GDBusConnection *conn,
        _D("%s - %d", signal, val);
 
        /* invoke the each callback with value */
-       DD_LIST_FOREACH(device_cb_list[type], elem, cb_info)
+       DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info)
                cb_info->cb(type, (void*)val, cb_info->data);
 }
 //LCOV_EXCL_STOP
@@ -283,7 +283,7 @@ static int release_request(device_callback_e type)
 int device_add_callback(device_callback_e type, device_changed_cb cb, void *data)
 {
        struct device_cb_info *cb_info;
-       dd_list *elem;
+       dd_list *elem, *elem_next;
        int ret, n;
 
        if (type < 0 || type >= DEVICE_CALLBACK_MAX)
@@ -301,7 +301,7 @@ int device_add_callback(device_callback_e type, device_changed_cb cb, void *data
        }
 
        /* check for the same request */
-       DD_LIST_FOREACH(device_cb_list[type], elem, cb_info) {
+       DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info) {
                if (cb_info->cb == cb)
                        return DEVICE_ERROR_ALREADY_IN_PROGRESS;
        }
@@ -322,7 +322,7 @@ int device_add_callback(device_callback_e type, device_changed_cb cb, void *data
 int device_remove_callback(device_callback_e type, device_changed_cb cb)
 {
        struct device_cb_info *cb_info;
-       dd_list *elem;
+       dd_list *elem, *elem_next;
        int ret, n;
 
        if (type < 0 || type >= DEVICE_CALLBACK_MAX)
@@ -332,7 +332,7 @@ int device_remove_callback(device_callback_e type, device_changed_cb cb)
                return DEVICE_ERROR_INVALID_PARAMETER;
 
        /* search for the same element with callback */
-       DD_LIST_FOREACH(device_cb_list[type], elem, cb_info) {
+       DD_LIST_FOREACH_SAFE(device_cb_list[type], elem, elem_next, cb_info) {
                if (cb_info->cb == cb)
                        break;
        }
index 0b7f559..a0ce513 100644 (file)
@@ -100,13 +100,13 @@ int device_haptic_get_count(int *device_number)
 //LCOV_EXCL_START Not called Callback
 void restart_callback(void)
 {
-       dd_list *elem;
+       dd_list *elem, *elem_next;
        char str_index[32];
        char *arr[1];
        struct haptic_handle *temp;
        int ret;
 
-       DD_LIST_FOREACH(handle_list, elem, temp) {
+       DD_LIST_FOREACH_SAFE(handle_list, elem, elem_next, temp) {
                snprintf(str_index, sizeof(str_index), "%d", temp->index);
                arr[0] = str_index;
                ret = dbus_method_sync(VIBRATOR_BUS_NAME,
@@ -124,7 +124,7 @@ void restart_callback(void)
 
 int device_haptic_open(int device_index, haptic_device_h *device_handle)
 {
-       dd_list *elem;
+       dd_list *elem, *elem_next;
        struct haptic_handle *handle;
        struct haptic_handle *temp;
        char str_index[32];
@@ -156,7 +156,7 @@ int device_haptic_open(int device_index, haptic_device_h *device_handle)
        if (ret < 0)
                return errno_to_device_error(ret); //LCOV_EXCL_LINE System Error
 
-       DD_LIST_FOREACH(handle_list, elem, temp) {
+       DD_LIST_FOREACH_SAFE(handle_list, elem, elem_next, temp) {
                if (temp->handle == ret) {
                        found = true;
                        temp->index = device_index;
@@ -187,7 +187,7 @@ int device_haptic_open(int device_index, haptic_device_h *device_handle)
 
 int device_haptic_close(haptic_device_h device_handle)
 {
-       dd_list *elem;
+       dd_list *elem, *elem_next;
        struct haptic_handle *handle = (struct haptic_handle *)device_handle;
        struct haptic_handle *temp;
        char str_handle[32];
@@ -202,7 +202,7 @@ int device_haptic_close(haptic_device_h device_handle)
        if (!ret)
                return DEVICE_ERROR_NOT_SUPPORTED;
 
-       DD_LIST_FOREACH(handle_list, elem, temp) {
+       DD_LIST_FOREACH_SAFE(handle_list, elem, elem_next, temp) {
                if (temp->handle != handle->handle)
                        continue;
                found = true;
@@ -234,7 +234,7 @@ int device_haptic_close(haptic_device_h device_handle)
 
 int device_haptic_vibrate(haptic_device_h device_handle, int duration, int feedback, haptic_effect_h *effect_handle)
 {
-       dd_list *elem;
+       dd_list *elem, *elem_next;
        struct haptic_handle *handle = (struct haptic_handle *)device_handle;
        struct haptic_handle *temp;
        char str_handle[32];
@@ -260,7 +260,7 @@ int device_haptic_vibrate(haptic_device_h device_handle, int duration, int feedb
 
        priority = HAPTIC_PRIORITY_MIN;
 
-       DD_LIST_FOREACH(handle_list, elem, temp) {
+       DD_LIST_FOREACH_SAFE(handle_list, elem, elem_next, temp) {
                if (temp != handle)
                        continue;
                found = true;
@@ -293,7 +293,7 @@ int device_haptic_vibrate(haptic_device_h device_handle, int duration, int feedb
 
 int device_haptic_stop(haptic_device_h device_handle, haptic_effect_h effect_handle)
 {
-       dd_list *elem;
+       dd_list *elem, *elem_next;
        struct haptic_handle *handle = (struct haptic_handle *)device_handle;
        struct haptic_handle *temp;
        char str_handle[32];
@@ -308,7 +308,7 @@ int device_haptic_stop(haptic_device_h device_handle, haptic_effect_h effect_han
        if (!ret)
                return DEVICE_ERROR_NOT_SUPPORTED;
 
-       DD_LIST_FOREACH(handle_list, elem, temp) {
+       DD_LIST_FOREACH_SAFE(handle_list, elem, elem_next, temp) {
                if (temp != handle)
                        continue;
                found = true;