-
Notifications
You must be signed in to change notification settings - Fork 304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DAOS-6685 Test: Fixed coverity issue in test code. #4684
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2299998
DAOS-6685 Test: Fixed coverity issue in test code.
ravalsam 8dc4ab8
Merge branch 'master' into samirrav/DAOS-6685_1
ravalsam 691d481
Updated code based on review comments.
ravalsam 35458c6
Apply suggestions from code review
ravalsam 0f17012
Update tests_dmg_helpers.c
ravalsam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is json_object_to_json_string null terminated? Same question as above why subtracting 1 from sizeof? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so. I tested it locally and it worked. |
||
|
||
if (!json_object_object_get_ex(dev, "rank", &tmp)) { | ||
D_ERROR("unable to extract rank from JSON\n"); | ||
|
@@ -656,19 +653,17 @@ 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) { | ||
D_DEBUG(DB_TEST, "key:\"%s\",val=%s\n", key, | ||
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; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sizeof(devices[*disks].host) - 1) : Not clear why you are subtracting 1
strtok(host, ":") + 1, : Are you null terminating here...
Not clear why 1 byte is added in strtok and one byte removed from sizeof... Maybe a comment about what is going on here could be helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 from strtok is needed to eliminate the double quotes from hostname, for example, JSON output is
"wolf-154:10001" so strtok will return "wolf-154 and +1 will start from second-string.
I used -1 to avoid the buffer overwrite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't Coverity still complain about this?
jvolivie-desk1:[~/daos]$ cat foo.c
#include <stdio.h>
#include <string.h>
int main()
{
char x[] = "";
char y[] = ":";
char z[] = ":abc";
char m[] = "abc:";
char n[] = "abc:cba";
}
I get
bc
bc
bc
Segmentation fault (core dumped)
Additionally, with strncpy, the string will not be null terminated if the length given would result in truncation. Typically, it's good practice to explicitly make sure there is a 0 in the last character.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not if that will complain about strtok or not. Let me find another way of copying it with null termination.