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

Conversation

devreal
Copy link
Contributor

@devreal devreal commented Mar 10, 2021

This silences a warning issued by GCC 10.2.0, warning about str[--str_len] = '\0' writing out of bounds if str_len is 0:

../../../opal/util/argv.c: In function ‘opal_argv_join_range’:
../../../opal/util/argv.c:368:20: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]
  368 |     str[--str_len] = '\0';
      |     ~~~~~~~~~~~~~~~^~~~~~
../../../opal/util/argv.c:363:32: note: at offset -1 to an object with size 0 allocated by ‘malloc’ here
  363 |     if (NULL == (str = (char*) malloc(str_len)))
      |                                ^~~~~~~~~~~~~~~

This also ensures that 0 is not passed to malloc, which may return NULL and lead to a unintended return of NULL instead of an empty string.

The change to greater-or-equal in (int)start >= opal_argv_count(argv) is meant to catch the case of argv[start] == NULL early.

Signed-off-by: Joseph Schuchart [email protected]

…in_range

This silences a warning issued by GCC 10.2.0, warning about `str[--str_len]
= '\0'` writing out of bounds if str_len is 0. This also ensures that
0 is not passed to malloc, which may return NULL and lead to a unintended
return value.

Signed-off-by: Joseph Schuchart <[email protected]>
@@ -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.

@jsquyres
Copy link
Member

Good catch!

...that being said, do we use opal_argv_join_range() anywhere in the code base? This feels very much like a "Jeff tried to be complete" kind of function...

After some grep'ing, it doesn't look like we use this function anywhere. Would a better solution be just to remove this function altogether?

@devreal
Copy link
Contributor Author

devreal commented Mar 10, 2021

Sure, I'm happy to remove it altogether. It won't hurt to keep it in case someone somewhere at some point wants that functionality though ^^

@jsquyres
Copy link
Member

Sure, I'm happy to remove it altogether. It won't hurt to keep it in case someone somewhere at some point wants that functionality though ^^

Your call. Looks like it's used in PMIx and we used to use it in the rsh PML (which is now PRTE). I.e., we don't use it in Open MPI anywhere.

@devreal
Copy link
Contributor Author

devreal commented Mar 10, 2021

I'd say we leave it in, it might be handy for debugging purposes and there is no harm in having it.

@awlauria awlauria merged commit 7f4f8fd into open-mpi:master Mar 11, 2021
@devreal
Copy link
Contributor Author

devreal commented Mar 11, 2021

I believe I have seen this warning with PRRTE and PMIx, too @rhc54

@rhc54
Copy link
Contributor

rhc54 commented Mar 11, 2021

Thanks - I have been tracking this PPR and plan to backport it to both of those. Appreciate you ensuring I saw it!

rhc54 added a commit to rhc54/prrte that referenced this pull request Mar 11, 2021
rhc54 added a commit to rhc54/openpmix that referenced this pull request Mar 11, 2021
rhc54 added a commit to rhc54/openpmix that referenced this pull request Mar 11, 2021
rhc54 added a commit to rhc54/prrte that referenced this pull request Mar 13, 2021
Tracks open-mpi/ompi#8581

Signed-off-by: Ralph Castain <[email protected]>
(cherry picked from commit f206379)
rhc54 added a commit to rhc54/openpmix that referenced this pull request Mar 16, 2021
Tracks open-mpi/ompi#8581

Signed-off-by: Ralph Castain <[email protected]>
(cherry picked from commit de48293)
rhc54 added a commit to openpmix/openpmix that referenced this pull request Apr 25, 2021
Tracks open-mpi/ompi#8581

Signed-off-by: Ralph Castain <[email protected]>
(cherry picked from commit de48293)
(cherry picked from commit 037f42c)
@devreal devreal deleted the fix-opal-argv-warn branch October 3, 2022 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants