Skip to content

Commit

Permalink
nolibxml: properly handle malloc/realloc() errors during import/export
Browse files Browse the repository at this point in the history
Refs #202.
  • Loading branch information
bgoglin committed Aug 10, 2016
1 parent 0798974 commit 8a63a9a
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions hwloc/topology-xml-nolibxml.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ hwloc_nolibxml_read_file(const char *xmlpath, char **bufferp, size_t *buflenp)
FILE * file;
size_t buflen, offset, readlen;
struct stat statbuf;
char *buffer;
char *buffer, *tmp;
size_t ret;

if (!strcmp(xmlpath, "-"))
Expand Down Expand Up @@ -350,9 +350,10 @@ hwloc_nolibxml_read_file(const char *xmlpath, char **bufferp, size_t *buflenp)
break;

buflen *= 2;
buffer = realloc(buffer, buflen+1);
if (!buffer)
goto out_with_file;
tmp = realloc(buffer, buflen+1);
if (!tmp)
goto out_with_buffer;
buffer = tmp;
readlen = buflen/2;
}

Expand All @@ -361,6 +362,8 @@ hwloc_nolibxml_read_file(const char *xmlpath, char **bufferp, size_t *buflenp)
*buflenp = offset+1;
return 0;

out_with_buffer:
free(buffer);
out_with_file:
fclose(file);
out:
Expand Down Expand Up @@ -681,10 +684,17 @@ hwloc_nolibxml_export_buffer(hwloc_topology_t topology, char **bufferp, int *buf

bufferlen = 16384; /* random guess for large enough default */
buffer = malloc(bufferlen);
if (!buffer)
return -1;
res = hwloc___nolibxml_prepare_export(topology, buffer, (int)bufferlen);

if (res > bufferlen) {
buffer = realloc(buffer, res);
char *tmp = realloc(buffer, res);
if (!tmp) {
free(buffer);
return -1;
}
buffer = tmp;
hwloc___nolibxml_prepare_export(topology, buffer, (int)res);
}

Expand Down Expand Up @@ -773,10 +783,17 @@ hwloc_nolibxml_export_diff_buffer(hwloc_topology_diff_t diff, const char *refnam

bufferlen = 16384; /* random guess for large enough default */
buffer = malloc(bufferlen);
if (!buffer)
return -1;
res = hwloc___nolibxml_prepare_export_diff(diff, refname, buffer, (int)bufferlen);

if (res > bufferlen) {
buffer = realloc(buffer, res);
char *tmp = realloc(buffer, res);
if (!tmp) {
free(buffer);
return -1;
}
buffer = tmp;
hwloc___nolibxml_prepare_export_diff(diff, refname, buffer, (int)res);
}

Expand Down

0 comments on commit 8a63a9a

Please sign in to comment.