From: Seonah Moon Date: Tue, 19 Oct 2021 10:52:41 +0000 (+0900) Subject: Check datapath handle before invoking user callback X-Git-Tag: submit/tizen/20211028.034129~7 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F47%2F265447%2F5;p=platform%2Fcore%2Fapi%2Fvine.git Check datapath handle before invoking user callback Change-Id: Icf8f93326397a7372b0ed1c8b5ad8eb9a15efcf9 --- diff --git a/src/vine-data-path.cpp b/src/vine-data-path.cpp index 281059f..b11baa1 100755 --- a/src/vine-data-path.cpp +++ b/src/vine-data-path.cpp @@ -49,6 +49,8 @@ struct { {NULL, NULL, NULL, NULL, NULL, NULL, NULL}, NULL}, }; +const int VALIDATOR_BITS = 0x12345678; + typedef struct { vine_data_path_method_e method; vine_dp_plugin_fn *plugin_fn; @@ -76,8 +78,20 @@ typedef struct { established_notifier established; vine_data_path_h listen_dp; // only for client dp in the server side + + int validator; } vine_data_path_s; +inline bool data_path_validate(vine_data_path_s *datapath, const char *func) +{ + RET_VAL_IF(datapath == NULL, false, "datapath is NULL"); + RET_VAL_IF(datapath->plugin_handle == NULL, false, "plugin_handle is NULL"); + RET_VAL_IF((((int)datapath->plugin_handle) ^ ((int)datapath->validator)) != VALIDATOR_BITS, false, + "validator fails. plugin_handle[%x] validator[%x]", datapath->plugin_handle, datapath->validator); + + return true; +} + typedef struct { int result; int port; @@ -206,7 +220,7 @@ static void __invoke_accepted_user_cb(void *event, void *user_data) VINE_LOGD("user callback is invoked by event queue."); - if (listen_dp && listen_dp->accepted_cb) + if (data_path_validate(listen_dp, __FUNCTION__) && listen_dp->accepted_cb) listen_dp->accepted_cb(accepted_event->connected_dp, listen_dp->accepted_cb_data); } @@ -233,7 +247,7 @@ void notify_accepted(vine_data_path_h datapath, int result) vine_data_path_s *listen_dp = (vine_data_path_s *)dp->listen_dp; VINE_LOGD("listen_dp[%p]", listen_dp); - if (listen_dp) + if (data_path_validate(listen_dp, __FUNCTION__)) vine_event_loop_add_event(listen_dp->event_queue, accepted_event, __invoke_accepted_user_cb, __free_accepted_event, listen_dp); } @@ -247,14 +261,18 @@ static void __invoke_opened_user_cb(void *event, void *user_data) VINE_LOGD("user callback is invoked by event queue"); - if (dp && dp->opened_cb) + if (data_path_validate(dp, __FUNCTION__) && dp->opened_cb) dp->opened_cb(dp, opened_event->result, opened_event->port, dp->opened_cb_data); } static void __opened_cb(int result, int port, void *user_data) { + VINE_LOGD("__opened_cb result[%d] port[%d] datapath[%p]", result, port, user_data); RET_IF(user_data == NULL, "dp is NULL"); vine_data_path_s *dp = (vine_data_path_s *)user_data; + if (!data_path_validate(dp, __FUNCTION__)) + return; + vine_dp_opened_event *opened_event = (vine_dp_opened_event *)calloc(1, sizeof(vine_dp_opened_event)); opened_event->result = result; @@ -285,6 +303,8 @@ static void __accepted_cb(vine_dp_addr_family_e addr_family, char *addr, RET_IF(user_data == NULL, "listen_dp is NULL"); vine_data_path_s *listen_dp = (vine_data_path_s *)user_data; + if (!data_path_validate(listen_dp, __FUNCTION__)) + return; VINE_LOGI("listen_dp[%p], security[%p]", listen_dp, listen_dp->security); vine_data_path_s *connected_dp = _vine_data_path_create(listen_dp->method, @@ -310,7 +330,7 @@ static void __invoke_connected_user_cb(void *event, void *user_data) VINE_LOGD("user callback is invoked by event queue."); - if (dp && dp->connected_cb) + if (data_path_validate(dp, __FUNCTION__) && dp->connected_cb) dp->connected_cb(dp, connected_event->result, dp->connected_cb_data); } @@ -325,8 +345,9 @@ void notify_connected(vine_data_path_h datapath, int result) connected_event->result = result; vine_data_path_s *dp = (vine_data_path_s *)datapath; - vine_event_loop_add_event(dp->event_queue, connected_event, - __invoke_connected_user_cb, free, datapath); + if (data_path_validate(dp, __FUNCTION__)) + vine_event_loop_add_event(dp->event_queue, connected_event, + __invoke_connected_user_cb, free, datapath); } static void __connected_cb(int result, void *user_data) @@ -336,6 +357,9 @@ static void __connected_cb(int result, void *user_data) RET_IF(user_data == NULL, "dp is NULL"); vine_data_path_s *dp = (vine_data_path_s *)user_data; + if (!data_path_validate(dp, __FUNCTION__)) + return; + if (result < 0) { VINE_LOGE("Fail to connect"); notify_connected(dp, result); @@ -356,7 +380,7 @@ static void __invoke_received_user_cb(void *event, void *user_data) VINE_LOGD("user callback is invoked by event queue."); - if (dp && dp->recv_cb) { + if (data_path_validate(dp, __FUNCTION__) && dp->recv_cb) { // user can read the data in recv_cb(). dp->recv_cb(dp, received_event->bytes, dp->recv_cb_data); } @@ -373,9 +397,11 @@ void notify_data_received(vine_data_path_h datapath, size_t bytes) VINE_LOGD("Create a received_event[%p] datapath[%p]", received_event, datapath); vine_data_path_s *dp = (vine_data_path_s *)datapath; - if (dp) + if (data_path_validate(dp, __FUNCTION__)) vine_event_loop_add_event(dp->event_queue, received_event, - __invoke_received_user_cb, free, datapath); + __invoke_received_user_cb, free, datapath); + else + free(received_event); } static void __received_cb(size_t bytes, void *user_data) @@ -383,6 +409,9 @@ static void __received_cb(size_t bytes, void *user_data) RET_IF(user_data == NULL, "dp is NULL"); vine_data_path_s *dp = (vine_data_path_s *)user_data; + if (!data_path_validate(dp, __FUNCTION__)) + return; + dp->state->received_cb(bytes); } @@ -390,6 +419,9 @@ static void __written_cb(int bytes, void *user_data) { VINE_LOGD("%d bytes are written through datapath[%p].", bytes, user_data); vine_data_path_s *dp = (vine_data_path_s *)user_data; + if (!data_path_validate(dp, __FUNCTION__)) + return; + dp->state->written_cb(bytes); } @@ -401,6 +433,9 @@ static void __invoke_terminated_user_cb(void *event, void *user_data) VINE_LOGD("user callback is invoked by event queue."); + if (!data_path_validate(dp, __FUNCTION__)) + return; + start_default_state(dp, dp->plugin_handle, *dp->plugin_fn, dp->state); if (dp->terminated_cb) dp->terminated_cb(dp, dp->terminated_cb_data); @@ -409,7 +444,7 @@ static void __invoke_terminated_user_cb(void *event, void *user_data) static void __terminated_cb(void *user_data) { vine_data_path_s *dp = (vine_data_path_s *)user_data; - if (dp) + if (data_path_validate(dp, __FUNCTION__)) vine_event_loop_add_event(dp->event_queue, NULL, __invoke_terminated_user_cb, NULL, user_data); } @@ -489,9 +524,14 @@ int vine_data_path_close(vine_data_path_h datapath) RET_VAL_IF(!dp->plugin_fn || !dp->plugin_fn->close, VINE_ERROR_INVALID_PARAMETER, "plugin_fn is NULL"); + //start_default_state(dp, dp->plugin_handle, *dp->plugin_fn, dp->state); + // Unset callbacks related with vine_dp_open() before closing to avoid timing issue. dp->opened_cb = NULL; + dp->accepted_cb = NULL; dp->connected_cb = NULL; + dp->recv_cb = NULL; + dp->terminated_cb = NULL; dp->plugin_fn->close(dp->plugin_handle); return VINE_ERROR_NONE; @@ -553,7 +593,9 @@ static vine_data_path_s *_vine_data_path_create(vine_data_path_method_e method, } dp->state = vine_get_default_state(dp, dp->plugin_handle, *dp->plugin_fn); - VINE_LOGD("datapath[%p] is created. method[%d]", dp, method); + dp->validator = (((int)dp->plugin_handle) ^ VALIDATOR_BITS); + VINE_LOGD("datapath[%p] is created. method[%d] validator[%x] plugin_handle[%p]", + dp, method, dp->validator, dp->plugin_handle); return dp; } @@ -562,6 +604,8 @@ int vine_data_path_destroy(vine_data_path_h datapath) RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL"); vine_data_path_s *dp = (vine_data_path_s *)datapath; + start_default_state(dp, dp->plugin_handle, *dp->plugin_fn, dp->state); + if (dp->plugin_fn && dp->plugin_fn->destroy) dp->plugin_fn->destroy(dp->plugin_handle); @@ -578,6 +622,7 @@ int vine_data_path_destroy(vine_data_path_h datapath) dp->connected_cb = NULL; dp->recv_cb = NULL; dp->terminated_cb = NULL; + dp->validator = 0; VINE_LOGD("data_path[%p] is destroyed", datapath); free(datapath); @@ -620,6 +665,9 @@ static void __established(vine_data_path_h datapath, int result) RET_IF(datapath == NULL, "datapath is NULL"); vine_data_path_s *dp = (vine_data_path_s *)datapath; + if (!data_path_validate(dp, __FUNCTION__)) + return; + dp->established(dp, result); }