From 1ba7d24e02e5694f793029bc6a16cc9ab2c38610 Mon Sep 17 00:00:00 2001 From: Minchul Lee Date: Wed, 10 Jun 2015 17:39:54 +0900 Subject: [PATCH] fix prevent issues Change-Id: Iab559d9d43a3fc2c62800a4152e0a216bd338e0d Signed-off-by: Minchul Lee --- lib/ic-client.c | 10 +++++++- lib/ic-ioty-repr.cpp | 11 ++++++--- lib/ic-ioty-repr.h | 2 +- lib/ic-ioty.cpp | 63 ++++++++++++++++++++++++++++++++++++------------- lib/ic-repr-list.c | 9 ++++++- lib/ic-repr-obj.c | 1 - lib/ic-repr.c | 19 +++++++++++++-- lib/ic-resource-types.c | 7 ++++-- lib/ic-response.c | 5 ++++ test/crud-test-client.c | 2 ++ test/crud-test-server.c | 15 ++++++++++++ test/repr-test-client.c | 9 +++---- test/repr-test-server.c | 9 +++++++ 13 files changed, 130 insertions(+), 32 deletions(-) diff --git a/lib/ic-client.c b/lib/ic-client.c index c84a563..a3af898 100644 --- a/lib/ic-client.c +++ b/lib/ic-client.c @@ -86,7 +86,10 @@ API void iotcon_client_free(iotcon_client_h resource) free(resource->uri); free(resource->host); - ic_options_free(resource->header_options); + + /* null COULD be allowed */ + if (resource->header_options) + ic_options_free(resource->header_options); iotcon_resource_types_free(resource->types); free(resource); } @@ -190,6 +193,11 @@ API int iotcon_client_set_options(iotcon_client_h resource, resource->header_options = ic_options_ref(header_options); else resource->header_options = header_options; + if (NULL == resource->header_options) { + ERR("header_options is NULL"); + return IOTCON_ERROR_NO_DATA; + } + resource->header_options->has_parent = true; return IOTCON_ERROR_NONE; diff --git a/lib/ic-ioty-repr.cpp b/lib/ic-ioty-repr.cpp index 4fa606a..201d870 100644 --- a/lib/ic-ioty-repr.cpp +++ b/lib/ic-ioty-repr.cpp @@ -81,6 +81,8 @@ iotcon_repr_h ic_ioty_repr_generate_repr(const OCRepresentation& ocRep) iotcon_repr_h repr_child = _ic_ioty_repr_create_child(ocChild); if (NULL == repr_child) { ERR("_ic_ioty_repr_create_child() Fail"); + /* free parent because adding child is failed. + * this func also will free children */ iotcon_repr_free(repr_parent); return NULL; } @@ -91,10 +93,11 @@ iotcon_repr_h ic_ioty_repr_generate_repr(const OCRepresentation& ocRep) return repr_parent; } -OCRepresentation ic_ioty_repr_parse(iotcon_repr_h repr) +int ic_ioty_repr_parse(iotcon_repr_h repr, OCRepresentation &ocRep) { FN_CALL; - OCRepresentation ocRep; + + int ret = IOTCON_ERROR_NONE; MessageContainer info; /* TODO: It's better that iotcon_repr_h is changed to OCRepresentation at once. */ @@ -115,12 +118,14 @@ OCRepresentation ic_ioty_repr_parse(iotcon_repr_h repr) } else { ERR("Invalid parameter(%s)", repr_json); + ret = IOTCON_ERROR_INVALID_PARAMETER; } } catch (exception &e) { ERR("setJSONRepresentation() Fail(%s)", e.what()); + ret = IOTCON_ERROR_INVALID_PARAMETER; } free(repr_json); - return ocRep; + return ret; } diff --git a/lib/ic-ioty-repr.h b/lib/ic-ioty-repr.h index 4cd531b..8888130 100644 --- a/lib/ic-ioty-repr.h +++ b/lib/ic-ioty-repr.h @@ -21,7 +21,7 @@ void ic_ioty_repr_found_device_cb(const OC::OCRepresentation& ocRep); iotcon_repr_h ic_ioty_repr_generate_repr(const OC::OCRepresentation& ocRep); -OC::OCRepresentation ic_ioty_repr_parse(iotcon_repr_h repr); +int ic_ioty_repr_parse(iotcon_repr_h repr, OC::OCRepresentation &ocRep); #endif /* __IOT_CONNECTIVITY_MANAGER_INTERNAL_IOTIVITY_REPRESENTATION_H__ */ diff --git a/lib/ic-ioty.cpp b/lib/ic-ioty.cpp index 77cc7da..ef94ff7 100644 --- a/lib/ic-ioty.cpp +++ b/lib/ic-ioty.cpp @@ -102,19 +102,24 @@ namespace icIotivityHandler { void foundResource(shared_ptr resource) { struct ic_remote_resource resource_s = {0}; - - resource_s.uri = ic_utils_strdup(resource->uri().c_str()); - resource_s.host = ic_utils_strdup(resource->host().c_str()); - resource_s.is_observable = resource->isObservable(); resource_s.types = NULL; vector resource_types = resource->getResourceTypes(); if (0 < resource_types.size()) { resource_s.types = iotcon_resource_types_new(); + if (NULL == resource_s.types) { + ERR("iotcon_resource_types_new() Fail"); + return; + } + for (string &resource_type : resource_types) iotcon_resource_types_insert(resource_s.types, resource_type.c_str()); } + resource_s.uri = ic_utils_strdup(resource->uri().c_str()); + resource_s.host = ic_utils_strdup(resource->host().c_str()); + resource_s.is_observable = resource->isObservable(); + vector resource_interfaces = resource->getResourceInterfaces(); for (string &resource_interface : resource_interfaces) { if (IC_STR_EQUAL == resource_interface.compare(DEFAULT_INTERFACE)) @@ -493,12 +498,16 @@ static OCEntityHandlerResult _ic_ioty_request_handler( request_s.repr = ic_ioty_repr_generate_repr(ocRep); if (NULL == request_s.repr) { ERR("request_s.repr is NULL"); + if (request_s.header_options) + iotcon_options_free(request_s.header_options); + if (request_s.query) + iotcon_query_free(request_s.query); return OC_EH_ERROR; } } if (RequestFlag & request->getRequestHandlerFlag()) { - request_type = request->getRequestType().c_str(); + request_type = ic_utils_strdup(request->getRequestType().c_str()); if (NULL == request_type) { ERR("request_type is NULL"); if (request_s.repr) @@ -827,6 +836,7 @@ extern "C" int ic_ioty_send_notify(OCResourceHandle resHandle, struct ic_notify_ int ret; OCStackResult ocRet; ObservationIds obsIds; + OCRepresentation ocRep; string iface; GList *node = g_list_first((GList*)observers); @@ -840,7 +850,11 @@ extern "C" int ic_ioty_send_notify(OCResourceHandle resHandle, struct ic_notify_ shared_ptr resourceResponse(new OCResourceResponse()); resourceResponse->setErrorCode(msg->error_code); - OCRepresentation ocRep = ic_ioty_repr_parse(msg->repr); + ret = ic_ioty_repr_parse(msg->repr, ocRep); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_ioty_repr_parse() Fail(%d)", ret); + return ret; + } if (IOTCON_INTERFACE_NONE != msg->iface) { ret = _ic_ioty_convert_interface_flag(msg->iface, iface); @@ -871,8 +885,13 @@ extern "C" int ic_ioty_send_res_response_data(struct ic_resource_response *resp) string iface; int ret; OCStackResult ocRet; + OCRepresentation ocRep; - OCRepresentation ocRep = ic_ioty_repr_parse(resp->repr); + ret = ic_ioty_repr_parse(resp->repr, ocRep); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_ioty_repr_parse() Fail(%d)", ret); + return ret; + } auto pResponse = make_shared(); if (pResponse) { @@ -1112,7 +1131,8 @@ extern "C" int ic_ioty_put(iotcon_client_h resource, iotcon_repr_h repr, iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data) { FN_CALL; - OCStackResult ret; + int ret; + OCStackResult result; OCResource::Ptr ocResource; OCRepresentation ocRep; QueryParamsMap queryParams; @@ -1120,7 +1140,11 @@ extern "C" int ic_ioty_put(iotcon_client_h resource, iotcon_repr_h repr, if (query) iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void*)&queryParams); - ocRep = ic_ioty_repr_parse(repr); + ret = ic_ioty_repr_parse(repr, ocRep); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_ioty_repr_parse() Fail(%d)", ret); + return ret; + } ocResource = _ic_ioty_create_oc_resource(resource); @@ -1129,9 +1153,9 @@ extern "C" int ic_ioty_put(iotcon_client_h resource, iotcon_repr_h repr, PutCallback putCallback = bind(&icIotivityHandler::putObject::onPut, object, placeholders::_1, placeholders::_2, placeholders::_3); - ret = ocResource->put(ocRep, queryParams, putCallback); - if (OC_STACK_OK != ret) { - ERR("put() Fail(%d)", ret); + result = ocResource->put(ocRep, queryParams, putCallback); + if (OC_STACK_OK != result) { + ERR("put() Fail(%d)", result); return IOTCON_ERROR_IOTIVITY; } @@ -1142,7 +1166,8 @@ extern "C" int ic_ioty_post(iotcon_client_h resource, iotcon_repr_h repr, iotcon_query_h query, iotcon_on_cru_cb cb, void *user_data) { FN_CALL; - OCStackResult ret; + int ret; + OCStackResult ocRet; QueryParamsMap queryParams; OCRepresentation ocRep; OCResource::Ptr ocResource; @@ -1150,7 +1175,11 @@ extern "C" int ic_ioty_post(iotcon_client_h resource, iotcon_repr_h repr, if (query) iotcon_query_foreach(query, _ic_ioty_accumulate_query_map, (void*)&queryParams); - ocRep = ic_ioty_repr_parse(repr); + ret = ic_ioty_repr_parse(repr, ocRep); + if (IOTCON_ERROR_NONE != ret) { + ERR("ic_ioty_repr_parse() Fail(%d)", ret); + return ret; + } ocResource = _ic_ioty_create_oc_resource(resource); @@ -1159,9 +1188,9 @@ extern "C" int ic_ioty_post(iotcon_client_h resource, iotcon_repr_h repr, PostCallback postCallback = bind(&icIotivityHandler::postObject::onPost, object, placeholders::_1, placeholders::_2, placeholders::_3); - ret = ocResource->post(ocRep, queryParams, postCallback); - if (OC_STACK_OK != ret) { - ERR("post() Fail(%d)", ret); + ocRet = ocResource->post(ocRep, queryParams, postCallback); + if (OC_STACK_OK != ocRet) { + ERR("post() Fail(%d)", ocRet); return IOTCON_ERROR_IOTIVITY; } diff --git a/lib/ic-repr-list.c b/lib/ic-repr-list.c index a993944..ca8bfa4 100644 --- a/lib/ic-repr-list.c +++ b/lib/ic-repr-list.c @@ -750,7 +750,10 @@ iotcon_list_h ic_list_from_json(JsonArray *parray) int count = json_array_get_length(parray); iotcon_list_h list = _ic_list_new(IOTCON_TYPE_NONE); - /* DBG("array count(%d)", count); */ + if (NULL == list) { + ERR("_ic_list_new() Fail"); + return NULL; + } for (i = 0; i < count; i++) { JsonNode *child_node = json_array_get_element(parray, i); @@ -874,6 +877,10 @@ static int _ic_list_clone_value(iotcon_list_h list, iotcon_list_h ret_list) count = g_list_length(list->list); for (i = 0; i < count; i++) { value = _ic_list_get_nth_value(list, i); + if (NULL == value) { + ERR("_ic_list_get_nth_value() Fail"); + return IOTCON_ERROR_INVALID_PARAMETER; + } if (list->type != value->type) { ERR("Type Mismatching(list:%d, value:%d)", list->type, value->type); return IOTCON_ERROR_INVALID_TYPE; diff --git a/lib/ic-repr-obj.c b/lib/ic-repr-obj.c index 4d5a8c6..7f3c287 100644 --- a/lib/ic-repr-obj.c +++ b/lib/ic-repr-obj.c @@ -599,7 +599,6 @@ static inline int _ic_obj_from_json(JsonObject *obj, GList *key_list, unsigned i RETV_IF(NULL == obj, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == key_list, IOTCON_ERROR_INVALID_PARAMETER); - RETV_IF(index < 0, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == ret_repr, IOTCON_ERROR_INVALID_PARAMETER); key = g_list_nth_data(key_list, index); diff --git a/lib/ic-repr.c b/lib/ic-repr.c index d99b740..62d0d17 100644 --- a/lib/ic-repr.c +++ b/lib/ic-repr.c @@ -479,6 +479,13 @@ iotcon_repr_h ic_repr_parse_json(const char *json_string) if (0 < rt_count) { res_types = iotcon_resource_types_new(); + if (NULL == res_types) { + ERR("iotcon_resource_types_new() Fail"); + iotcon_repr_free(repr); + g_object_unref(parser); + return NULL; + } + for (rt_index = 0; rt_index < rt_count; rt_index++) { rtype_str = json_array_get_string_element(rt_array, rt_index); iotcon_resource_types_insert(res_types, rtype_str); @@ -530,7 +537,7 @@ API void iotcon_repr_free(iotcon_repr_h repr) /* (GDestroyNotify) : iotcon_repr_h is proper type than gpointer */ g_list_free_full(repr->children, (GDestroyNotify)iotcon_repr_free); - /* repr->res_types COULD be not null */ + /* null COULD be allowed */ if (repr->res_types) iotcon_resource_types_free(repr->res_types); g_hash_table_destroy(repr->hash_table); @@ -543,6 +550,7 @@ static void _ic_repr_obj_clone(char *key, iotcon_value_h src_val, iotcon_repr_h { FN_CALL; int type, ret; + char *dup_key; iotcon_value_h value, copied_val; iotcon_list_h child_list, copied_list; iotcon_repr_h child_repr, copied_repr; @@ -559,7 +567,14 @@ static void _ic_repr_obj_clone(char *key, iotcon_value_h src_val, iotcon_repr_h ERR("ic_value_clone() Fail"); return; } - ic_obj_set_value(dest_repr, ic_utils_strdup(key), copied_val); + + dup_key = ic_utils_strdup(key); + if (NULL == dup_key) { + ERR("dupic_utils_strdup() Fail"); + return; + } + + ic_obj_set_value(dest_repr, dup_key, copied_val); break; case IOTCON_TYPE_LIST: ret = ic_value_get_list(src_val, &child_list); diff --git a/lib/ic-resource-types.c b/lib/ic-resource-types.c index abb86b6..6b28fb7 100644 --- a/lib/ic-resource-types.c +++ b/lib/ic-resource-types.c @@ -73,6 +73,9 @@ static bool _ic_resource_types_duplicate_check(iotcon_resource_types_h types, { GList *ret = NULL; + RETV_IF(NULL == types, false); + RETV_IF(NULL == type, false); + ret = g_list_find_custom(types->type_list, type, _ic_resource_types_strcmp); if (NULL == ret) return false; @@ -81,13 +84,13 @@ static bool _ic_resource_types_duplicate_check(iotcon_resource_types_h types, } -/* If you want to make a new list, then you should set res_types is NULL. - * The length of resource type should be less than or equal to 61. +/* The length of resource type should be less than or equal to 61. * Duplicate strings are not allowed. */ API int iotcon_resource_types_insert(iotcon_resource_types_h types, const char *type) { char *resource_type; + RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER); RETV_IF(NULL == type, IOTCON_ERROR_INVALID_PARAMETER); RETVM_IF(1 < types->ref_count, IOTCON_ERROR_INVALID_PARAMETER, "Don't modify it. It is already set."); diff --git a/lib/ic-response.c b/lib/ic-response.c index 61d573d..8965f92 100644 --- a/lib/ic-response.c +++ b/lib/ic-response.c @@ -109,6 +109,11 @@ API int iotcon_response_set(iotcon_response_h resp, iotcon_response_property_e p resp->header_options = ic_options_ref(options); else resp->header_options = options; + if (NULL == resp->header_options) { + ERR("header_options is NULL"); + return IOTCON_ERROR_NO_DATA; + } + resp->header_options->has_parent = true; break; case IOTCON_RESPONSE_NONE: diff --git a/test/crud-test-client.c b/test/crud-test-client.c index 9b3aedd..f278cdd 100644 --- a/test/crud-test-client.c +++ b/test/crud-test-client.c @@ -98,6 +98,8 @@ static void _on_post(iotcon_options_h header_options, iotcon_repr_h recv_repr, new_door_resource = iotcon_client_new(host, created_uri, true, types, ifaces); iotcon_delete(new_door_resource, _on_delete, NULL); + + iotcon_client_free(new_door_resource); } static void _on_put(iotcon_options_h header_options, iotcon_repr_h recv_repr, diff --git a/test/crud-test-server.c b/test/crud-test-server.c index 7e5d46f..9527cd8 100644 --- a/test/crud-test-server.c +++ b/test/crud-test-server.c @@ -69,6 +69,11 @@ static iotcon_resource_h _create_door_resource(char *uri, iotcon_interface_e int iotcon_resource_property_e properties) { iotcon_resource_types_h resource_types = iotcon_resource_types_new(); + if (NULL == resource_types) { + ERR("iotcon_resource_types_new() Fail"); + return NULL; + } + int ret = iotcon_resource_types_insert(resource_types, my_door.type); if (IOTCON_ERROR_NONE != ret) { iotcon_resource_types_free(resource_types); @@ -111,6 +116,8 @@ static void _request_handler_get(iotcon_response_h response) iotcon_repr_set_bool(resp_repr, "opened", my_door.state); _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK); + + iotcon_repr_free(resp_repr); } static void _request_handler_put(iotcon_request_h request, iotcon_response_h response) @@ -136,6 +143,8 @@ static void _request_handler_put(iotcon_request_h request, iotcon_response_h res iotcon_repr_set_bool(resp_repr, "opened", my_door.state); _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK); + + iotcon_repr_free(resp_repr); } static void _request_handler_post(iotcon_response_h response) @@ -161,6 +170,8 @@ static void _request_handler_post(iotcon_response_h response) iotcon_repr_set_str(resp_repr, "createduri", "/a/door1"); _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_RESOURCE_CREATED); + + iotcon_repr_free(resp_repr); } static gboolean _notifier(gpointer user_data) @@ -174,6 +185,8 @@ static gboolean _notifier(gpointer user_data) iotcon_notimsg_h msg = iotcon_notimsg_new(repr, IOTCON_INTERFACE_DEFAULT); iotcon_notify(user_data, msg, observers); + iotcon_repr_free(repr); + return TRUE; } @@ -191,6 +204,8 @@ static void _request_handler_delete(iotcon_response_h response) /* add observe */ g_timeout_add_seconds(5, _notifier, door_handle); + + iotcon_repr_free(resp_repr); } static int _query_cb(const char *key, const char *value, void *user_data) diff --git a/test/repr-test-client.c b/test/repr-test-client.c index e00afec..afda58a 100644 --- a/test/repr-test-client.c +++ b/test/repr-test-client.c @@ -78,8 +78,10 @@ static void _on_get(iotcon_repr_h recv_repr, int response_result) } iotcon_repr_get_uri(child_repr, &uri); - if (uri) - DBG("uri : %s", uri); + if (NULL == uri) + continue; + + DBG("uri : %s", uri); if (!strcmp("/a/light", uri)) { key_count = iotcon_repr_get_keys_count(child_repr); @@ -88,8 +90,7 @@ static void _on_get(iotcon_repr_h recv_repr, int response_result) iotcon_repr_get_int(child_repr, "brightness", &brightness); DBG("brightness : %d", brightness); } - } - else if (!strcmp("/a/switch", uri)) { + } else if (!strcmp("/a/switch", uri)) { key_count = iotcon_repr_get_keys_count(child_repr); if (key_count) { bool bswitch; diff --git a/test/repr-test-server.c b/test/repr-test-server.c index e347ab2..38eae19 100644 --- a/test/repr-test-server.c +++ b/test/repr-test-server.c @@ -87,6 +87,7 @@ static void _room_request_handler_get(iotcon_request_h request, iotcon_list_insert_int(temperature_list, 25, -1); iotcon_list_insert_int(temperature_list, 26, -1); iotcon_repr_set_list(room_repr, "today_temp", temperature_list); + iotcon_list_free(temperature_list); /* create a light Representation */ light_repr = iotcon_repr_new(); @@ -106,6 +107,7 @@ static void _room_request_handler_get(iotcon_request_h request, ret = iotcon_request_get_query(request, &query); if (IOTCON_ERROR_NONE != ret) { ERR("iotcon_request_get_query() Fail(%d)", ret); + iotcon_repr_free(room_repr); return; } if (query) @@ -263,6 +265,11 @@ int main(int argc, char **argv) /* register room resource */ light_rtypes = iotcon_resource_types_new(); + if (NULL == light_rtypes) { + ERR("iotcon_resource_types_new() Fail"); + return -1; + } + iotcon_resource_types_insert(light_rtypes, "core.light"); iotcon_resource_h light_handle = iotcon_register_resource("/a/light", light_rtypes, (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH), @@ -279,6 +286,8 @@ int main(int argc, char **argv) return -1; } + iotcon_resource_types_free(light_rtypes); + g_main_loop_run(loop); g_main_loop_unref(loop); -- 2.7.4