Skip to content

Commit 0edb311

Browse files
authored
Merge pull request #11024 from drwootton/opal_hostname_leak
Fix potential memory leak in opal_init_gethostname
2 parents 710ff57 + 8c493a8 commit 0edb311

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)