Skip to content

Commit 8c493a8

Browse files
author
David Wootton
committed
Fix potential memory leak in opal_init_gethostname
Fix potential memory leak in opal_get_hostname, where, if the realloc call at the bottom of the loop fails, the previously allocated memory is not freed and is leaked. The linux realloc man page states that if realloc returns NULL then the memory pointed to by the first parameter to realloc is not freed. This is fixed by assigning the result of realloc to a temp pointer then assigning that temp pointer to the original pointer if realloc succeeds. Signed-off-by: David Wootton <dwootton@us.ibm.com>
1 parent 1e2e97c commit 8c493a8

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

opal/runtime/opal_init_core.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ int opal_init_gethostname(void)
337337
size_t count, length = OPAL_LOCAL_MAXHOSTNAMELEN;
338338
int ret_val, num_tries = 0;
339339

340+
char *newbuf;
340341
char *buf = calloc(1, length);
341342
if (NULL == buf) {
342343
return OPAL_ERR_OUT_OF_RESOURCE;
@@ -407,10 +408,12 @@ int opal_init_gethostname(void)
407408
* the buffer and try again.
408409
*/
409410
length *= 2;
410-
buf = realloc(buf, length);
411-
if (NULL == buf) {
411+
newbuf = realloc(buf, length);
412+
if (NULL == newbuf) {
413+
free(buf);
412414
return OPAL_ERR_OUT_OF_RESOURCE;
413415
}
416+
buf = newbuf;
414417
} /* end while */
415418

416419
/* If we got here, it means that we tried too many times and are

0 commit comments

Comments
 (0)