Skip to content

Make sure to never allocate zero-length memory buffer in opal_argv_join_range #8581

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

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions opal/util/argv.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ char *opal_argv_join_range(char **argv, size_t start, size_t end, int delimiter)

/* Bozo case */

if (NULL == argv || NULL == argv[0] || (int)start > opal_argv_count(argv)) {
if (NULL == argv || NULL == argv[0] || (int)start >= opal_argv_count(argv)) {
return strdup("");
Copy link
Member

Choose a reason for hiding this comment

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

We need to be consistent in this function when something didn't go as planned: either we return strdup("") or NULL, but consistently.

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 the point of returning NULL is the assumption that if malloc returns NULL we're out-of-memory, so strdup won't work either.

}

Expand All @@ -354,10 +354,15 @@ char *opal_argv_join_range(char **argv, size_t start, size_t end, int delimiter)
str_len += strlen(*p) + 1;
}

if (0 == str_len) {
return strdup("");
}

/* Allocate the string. */

if (NULL == (str = (char*) malloc(str_len)))
if (NULL == (str = (char*) malloc(str_len))) {
return NULL;
}

/* Loop filling in the string. */

Expand Down