From 2299998be6fc858846fafd8c982533058d9fd3bd Mon Sep 17 00:00:00 2001 From: Samir Raval Date: Thu, 18 Feb 2021 03:00:50 +0000 Subject: [PATCH 1/4] DAOS-6685 Test: Fixed coverity issue in test code. Fixed Coverity issue in test code. 313028(#2 of 2) and 313033(#1 of 1). Signed-off-by: Samir Raval --- src/common/tests_dmg_helpers.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/common/tests_dmg_helpers.c b/src/common/tests_dmg_helpers.c index 98b5799f46a..31fe79932ec 100644 --- a/src/common/tests_dmg_helpers.c +++ b/src/common/tests_dmg_helpers.c @@ -583,13 +583,8 @@ parse_device_info(struct json_object *smd_dev, device_list *devices, for (i = 0; i < dev_length; i++) { dev = json_object_array_get_idx(smd_dev, i); - if (strlen(host + 2) <= DSS_HOSTNAME_MAX_LEN) - strcpy(devices[*disks].host, strtok(host, ":") + 1); - else { - D_ERROR("Hostname is larger than %d\n", - DSS_HOSTNAME_MAX_LEN); - return -DER_INVAL; - } + strncpy(devices[*disks].host, strtok(host, ":") + 1, + sizeof(devices[*disks].host) - 1); if (!json_object_object_get_ex(dev, "uuid", &tmp)) { D_ERROR("unable to extract uuid from JSON\n"); @@ -615,7 +610,9 @@ parse_device_info(struct json_object *smd_dev, device_list *devices, D_ERROR("unable to extract state from JSON\n"); return -DER_INVAL; } - strcpy(devices[*disks].state, json_object_to_json_string(tmp)); + + strncpy(devices[*disks].state, json_object_to_json_string(tmp), + sizeof(devices[*disks].state) - 1); if (!json_object_object_get_ex(dev, "rank", &tmp)) { D_ERROR("unable to extract rank from JSON\n"); @@ -656,9 +653,8 @@ dmg_storage_device_list(const char *dmg_config_file, int *ndisks, if (!json_object_object_get_ex(dmg_out, "host_storage_map", &storage_map)) { - D_FREE(disk); D_ERROR("unable to extract host_storage_map from JSON\n"); - return -DER_INVAL; + D_GOTO(out, rc = -DER_INVAL); } json_object_object_foreach(storage_map, key, val) { @@ -666,9 +662,8 @@ dmg_storage_device_list(const char *dmg_config_file, int *ndisks, json_object_to_json_string(val)); if (!json_object_object_get_ex(val, "hosts", &hosts)) { - D_FREE(disk); D_ERROR("unable to extract hosts from JSON\n"); - return -DER_INVAL; + D_GOTO(out, rc = -DER_INVAL); } D_ALLOC(host, strlen(json_object_to_json_string(hosts)) + 1); @@ -684,7 +679,7 @@ dmg_storage_device_list(const char *dmg_config_file, int *ndisks, smd_info, "devices", &smd_dev)) { D_ERROR("unable to extract devices\n"); D_FREE(host); - return -DER_INVAL; + D_GOTO(out, rc = -DER_INVAL); } if (smd_dev != NULL) @@ -712,6 +707,7 @@ dmg_storage_device_list(const char *dmg_config_file, int *ndisks, if (dmg_out != NULL) json_object_put(dmg_out); +out: D_FREE(disk); return rc; } From 691d48162a2c553dafa39767e60c8d2e0224beee Mon Sep 17 00:00:00 2001 From: Samir Raval Date: Fri, 19 Feb 2021 18:04:47 +0000 Subject: [PATCH 2/4] Updated code based on review comments. Signed-off-by: Samir Raval --- src/common/tests_dmg_helpers.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/common/tests_dmg_helpers.c b/src/common/tests_dmg_helpers.c index 31fe79932ec..01b5c048604 100644 --- a/src/common/tests_dmg_helpers.c +++ b/src/common/tests_dmg_helpers.c @@ -579,12 +579,19 @@ parse_device_info(struct json_object *smd_dev, device_list *devices, struct json_object *targets; int tgts_len; int i, j; + char *tmp_var; for (i = 0; i < dev_length; i++) { dev = json_object_array_get_idx(smd_dev, i); - strncpy(devices[*disks].host, strtok(host, ":") + 1, - sizeof(devices[*disks].host) - 1); + tmp_var = strtok(host, ":") + 1; + if (!tmp_var) { + D_ERROR("Hostname is empty\n"); + return -DER_INVAL; + } + + snprintf(devices[*disks].host, sizeof(devices[*disks].host), + "%s", tmp_var); if (!json_object_object_get_ex(dev, "uuid", &tmp)) { D_ERROR("unable to extract uuid from JSON\n"); @@ -611,8 +618,8 @@ parse_device_info(struct json_object *smd_dev, device_list *devices, return -DER_INVAL; } - strncpy(devices[*disks].state, json_object_to_json_string(tmp), - sizeof(devices[*disks].state) - 1); + snprintf(devices[*disks].state, sizeof(devices[*disks].state), + "%s", json_object_to_json_string(tmp)); if (!json_object_object_get_ex(dev, "rank", &tmp)) { D_ERROR("unable to extract rank from JSON\n"); From 35458c69f6622ecbc4ca569ef95f58a0233b7a13 Mon Sep 17 00:00:00 2001 From: Samir Raval Date: Fri, 19 Feb 2021 10:24:57 -0800 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: daosbuild1 <47433925+daosbuild1@users.noreply.github.com> --- src/common/tests_dmg_helpers.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/tests_dmg_helpers.c b/src/common/tests_dmg_helpers.c index 01b5c048604..c4d37ee1276 100644 --- a/src/common/tests_dmg_helpers.c +++ b/src/common/tests_dmg_helpers.c @@ -591,7 +591,7 @@ parse_device_info(struct json_object *smd_dev, device_list *devices, } snprintf(devices[*disks].host, sizeof(devices[*disks].host), - "%s", tmp_var); + "%s", tmp_var); if (!json_object_object_get_ex(dev, "uuid", &tmp)) { D_ERROR("unable to extract uuid from JSON\n"); @@ -619,7 +619,7 @@ parse_device_info(struct json_object *smd_dev, device_list *devices, } snprintf(devices[*disks].state, sizeof(devices[*disks].state), - "%s", json_object_to_json_string(tmp)); + "%s", json_object_to_json_string(tmp)); if (!json_object_object_get_ex(dev, "rank", &tmp)) { D_ERROR("unable to extract rank from JSON\n"); From 0f17012a7b2a93948a328d61226540a0cf867f35 Mon Sep 17 00:00:00 2001 From: Samir Raval Date: Fri, 19 Feb 2021 16:38:23 -0800 Subject: [PATCH 4/4] Update tests_dmg_helpers.c --- src/common/tests_dmg_helpers.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/tests_dmg_helpers.c b/src/common/tests_dmg_helpers.c index c4d37ee1276..1e99b8a71cc 100644 --- a/src/common/tests_dmg_helpers.c +++ b/src/common/tests_dmg_helpers.c @@ -584,14 +584,14 @@ parse_device_info(struct json_object *smd_dev, device_list *devices, for (i = 0; i < dev_length; i++) { dev = json_object_array_get_idx(smd_dev, i); - tmp_var = strtok(host, ":") + 1; - if (!tmp_var) { + tmp_var = strtok(host, ":"); + if (tmp_var == NULL) { D_ERROR("Hostname is empty\n"); return -DER_INVAL; } snprintf(devices[*disks].host, sizeof(devices[*disks].host), - "%s", tmp_var); + "%s", tmp_var + 1); if (!json_object_object_get_ex(dev, "uuid", &tmp)) { D_ERROR("unable to extract uuid from JSON\n");