Skip to content
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 5 commits into from
Feb 26, 2021

Conversation

ravalsam
Copy link
Contributor

Fixed Coverity issue in the test code.
313028(#2 of 2) and 313033(#1 of 1).

Signed-off-by: Samir Raval [email protected]

Fixed Coverity issue in test code.
313028(#2 of 2) and 313033(#1 of 1).

Signed-off-by: Samir Raval <[email protected]>
Copy link
Collaborator

@daosbuild1 daosbuild1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. No errors found by checkpatch.

return -DER_INVAL;
}
strncpy(devices[*disks].host, strtok(host, ":") + 1,
sizeof(devices[*disks].host) - 1);
Copy link
Contributor

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.

Copy link
Contributor Author

@ravalsam ravalsam Feb 18, 2021

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.

Copy link
Contributor

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";

printf("%s\n", strtok(n, ":") + 1);
fflush(stdout);
printf("%s\n", strtok(m, ":") + 1);
fflush(stdout);
printf("%s\n", strtok(z, ":") + 1);
fflush(stdout);
printf("%s\n", strtok(y, ":") + 1);
fflush(stdout);
printf("%s\n", strtok(x, ":") + 1);
fflush(stdout);
return 0;

}

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.

Copy link
Contributor Author

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.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. I tested it locally and it worked.

Copy link
Collaborator

@daosbuild1 daosbuild1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. No errors found by checkpatch.

@ravalsam ravalsam requested a review from rpadma2 February 18, 2021 23:02
rpadma2
rpadma2 previously approved these changes Feb 18, 2021
GitHuaKuang
GitHuaKuang previously approved these changes Feb 19, 2021
@ravalsam ravalsam requested a review from a team February 19, 2021 16:43
return -DER_INVAL;
}
strncpy(devices[*disks].host, strtok(host, ":") + 1,
sizeof(devices[*disks].host) - 1);
Copy link
Contributor

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";

printf("%s\n", strtok(n, ":") + 1);
fflush(stdout);
printf("%s\n", strtok(m, ":") + 1);
fflush(stdout);
printf("%s\n", strtok(z, ":") + 1);
fflush(stdout);
printf("%s\n", strtok(y, ":") + 1);
fflush(stdout);
printf("%s\n", strtok(x, ":") + 1);
fflush(stdout);
return 0;

}

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.

@ravalsam ravalsam dismissed stale reviews from GitHuaKuang and rpadma2 via 691d481 February 19, 2021 18:05
@ravalsam ravalsam requested a review from jolivier23 February 19, 2021 18:06
src/common/tests_dmg_helpers.c Outdated Show resolved Hide resolved
src/common/tests_dmg_helpers.c Outdated Show resolved Hide resolved
@daosbuild1
Copy link
Collaborator

@daosbuild1 daosbuild1 dismissed their stale review February 19, 2021 18:27

Updated patch

Copy link
Collaborator

@daosbuild1 daosbuild1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. No errors found by checkpatch.

strncpy(devices[*disks].host, strtok(host, ":") + 1,
sizeof(devices[*disks].host) - 1);
tmp_var = strtok(host, ":") + 1;
if (!tmp_var) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if strtok returns NULL, this will be 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed the code to check NULL

Copy link
Collaborator

@daosbuild1 daosbuild1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. No errors found by checkpatch.

@ashleypittman
Copy link
Contributor

In general strtok_r() is strongly preferred over strtok(), however there may be an exception for test code.

@ravalsam ravalsam requested a review from jolivier23 February 22, 2021 17:27
@sylviachanoiyee sylviachanoiyee merged commit b0ac09c into master Feb 26, 2021
@sylviachanoiyee sylviachanoiyee deleted the samirrav/DAOS-6685_1 branch February 26, 2021 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

8 participants