From f51dbb4676e9bfb408d77b6ff04e4dea25f7bce2 Mon Sep 17 00:00:00 2001 From: Jaeyun Jung Date: Mon, 4 Mar 2024 17:09:36 +0900 Subject: [PATCH] [C-Api] check empty key value Code clean, check empty string case. Signed-off-by: Jaeyun Jung --- c/src/ml-api-common.c | 8 ++++++-- c/src/ml-api-internal.h | 2 ++ c/src/ml-api-service-agent-client.c | 4 +++- tests/capi/unittest_capi_inference_single.cc | 3 +++ tests/capi/unittest_capi_service_agent_client.cc | 10 +++------- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/c/src/ml-api-common.c b/c/src/ml-api-common.c index d022a9d..4487f84 100644 --- a/c/src/ml-api-common.c +++ b/c/src/ml-api-common.c @@ -1340,9 +1340,13 @@ _ml_info_set_value (ml_info_s * info, const char *key, void *value, { ml_info_value_s *info_value; - if (!info || !key || !value) + if (!STR_IS_VALID (key)) _ml_error_report_return (ML_ERROR_INVALID_PARAMETER, - "The parameter, 'info', 'key' or 'value' is NULL. It should be a valid ml_info, key and value."); + "The parameter, 'key' is invalid. It should be a valid string."); + + if (!info || !value) + _ml_error_report_return (ML_ERROR_INVALID_PARAMETER, + "The parameter, 'info' or 'value' is NULL. It should be a valid ml_info and value."); info_value = g_new0 (ml_info_value_s, 1); if (!info_value) diff --git a/c/src/ml-api-internal.h b/c/src/ml-api-internal.h index 6083cef..90532eb 100644 --- a/c/src/ml-api-internal.h +++ b/c/src/ml-api-internal.h @@ -21,6 +21,8 @@ extern "C" { #endif /* __cplusplus */ +#define STR_IS_VALID(s) ((s) && (s)[0] != '\0') + /** * DO NOT USE THE LOG INFRA of NNSTREAMER. * This header is supposed to be independent from nnstreamer.git diff --git a/c/src/ml-api-service-agent-client.c b/c/src/ml-api-service-agent-client.c index 1f2ac6e..5126fb3 100644 --- a/c/src/ml-api-service-agent-client.c +++ b/c/src/ml-api-service-agent-client.c @@ -227,7 +227,9 @@ _build_ml_info_from_json_cstr (const gchar * jcstring, void **handle) const gchar *key = l->data; const gchar *val = json_object_get_string_member (jobj, key); - _ml_information_set (_info, key, g_strdup (val), g_free); + /* Prevent empty string case. */ + if (STR_IS_VALID (key) && STR_IS_VALID (val)) + _ml_information_set (_info, key, g_strdup (val), g_free); } ret = _parse_app_info_and_update_path (_info); diff --git a/tests/capi/unittest_capi_inference_single.cc b/tests/capi/unittest_capi_inference_single.cc index 415736f..71e97d2 100644 --- a/tests/capi/unittest_capi_inference_single.cc +++ b/tests/capi/unittest_capi_inference_single.cc @@ -4111,6 +4111,9 @@ TEST (nnstreamer_capi_ml_option, test03_n) status = ml_option_set (option, NULL, value, NULL); EXPECT_EQ (status, ML_ERROR_INVALID_PARAMETER); + status = ml_option_set (option, "", value, NULL); + EXPECT_EQ (status, ML_ERROR_INVALID_PARAMETER); + status = ml_option_set (option, "key", NULL, NULL); EXPECT_EQ (status, ML_ERROR_INVALID_PARAMETER); diff --git a/tests/capi/unittest_capi_service_agent_client.cc b/tests/capi/unittest_capi_service_agent_client.cc index bcd2970..67eb75a 100644 --- a/tests/capi/unittest_capi_service_agent_client.cc +++ b/tests/capi/unittest_capi_service_agent_client.cc @@ -1051,6 +1051,7 @@ TEST_F (MLServiceAgentTest, model_ml_information_get_00_n) int status; const gchar *model_name = "some_model_name"; + const gchar *model_desc = "desc-model-test"; guint version; const gchar *root_path = g_getenv ("MLAPI_SOURCE_ROOT_PATH"); @@ -1069,7 +1070,7 @@ TEST_F (MLServiceAgentTest, model_ml_information_get_00_n) status = ml_service_model_delete (model_name, 0U); EXPECT_TRUE (status == ML_ERROR_NONE || status == ML_ERROR_INVALID_PARAMETER); - status = ml_service_model_register (model_name, test_model, true, NULL, &version); + status = ml_service_model_register (model_name, test_model, true, model_desc, &version); EXPECT_EQ (ML_ERROR_NONE, status); status = ml_service_model_get (model_name, version, &info_h); @@ -1083,7 +1084,7 @@ TEST_F (MLServiceAgentTest, model_ml_information_get_00_n) gchar *description; status = ml_information_get (info_h, "description", (void **) &description); EXPECT_EQ (ML_ERROR_NONE, status); - EXPECT_STREQ ("", description); + EXPECT_STREQ (model_desc, description); status = ml_information_get (info_h, key, NULL); @@ -1266,11 +1267,6 @@ TEST_F (MLServiceAgentTest, model_scenario) status = ml_information_get (_info_h, "path", (void **) &path); EXPECT_EQ (ML_ERROR_NONE, status); EXPECT_STREQ (path, test_model1); - - gchar *app_info; - status = ml_information_get (_info_h, "app_info", (void **) &app_info); - EXPECT_EQ (ML_ERROR_NONE, status); - EXPECT_STREQ (app_info, ""); } else if (g_ascii_strcasecmp (version_str, "2") == 0) { gchar *is_active; status = ml_information_get (_info_h, "active", (void **) &is_active); -- 2.7.4