Skip to content

Commit 2ef76b0

Browse files
committed
treematch: Fix warnings
Fix two classes of warnings in the treematch code. First, handle the error condition for fgets(), which may leave the buffer in an undefined state in error. Define the state of the buffer on error by initializing the first character to a NULL terminator, which will trip the already existing error handling. Second, handle errors from a call to hwloc that could cause very large allocations (due to -1 being cast to a size_t). Signed-off-by: Brian Barrett <bbarrett@amazon.com>
1 parent 053ce66 commit 2ef76b0

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

3rd-party/treematch/tm_topology.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ tm_topology_t * tgt_to_tm(char *filename)
6868
printf("Reading TGT file: %s\n",filename);
6969

7070

71-
fgets(line,1024,pf);
71+
if (NULL == fgets(line,1024,pf)) {
72+
/* either an error has occurred (and is in an unknown state) or
73+
we hit EOF and line is empty. Either way, make line the
74+
empty string to avoid errors later */
75+
line[0] = '\0';
76+
}
77+
7278
fclose(pf);
7379

7480
s = strstr(line,"tleaf");
@@ -159,7 +165,13 @@ double ** topology_to_arch(hwloc_topology_t topology)
159165
double **arch = NULL;
160166

161167
nb_proc = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_PU);
162-
arch = (double**)MALLOC(sizeof(double*)*nb_proc);
168+
if (nb_proc < 0) {
169+
return NULL;
170+
}
171+
arch = (double**)malloc(sizeof(double*)*nb_proc);
172+
if (NULL == arch) {
173+
return NULL;
174+
}
163175
for( i = 0 ; i < nb_proc ; i++ ){
164176
obj_proc1 = hwloc_get_obj_by_type(topology,HWLOC_OBJ_PU,i);
165177
arch[obj_proc1->os_index] = (double*)MALLOC(sizeof(double)*nb_proc);
@@ -534,7 +546,9 @@ int tm_topology_add_binding_constraints(char *constraints_filename, tm_topology
534546

535547
/* compute the size of the array to store the constraints*/
536548
n = 0;
537-
fgets(line, LINE_SIZE, pf);
549+
if (NULL == fgets(line, LINE_SIZE, pf)) {
550+
line[0] = '\0';
551+
}
538552
l = line;
539553
while((ptr=strtok(l," \t"))){
540554
l = NULL;
@@ -545,7 +559,9 @@ int tm_topology_add_binding_constraints(char *constraints_filename, tm_topology
545559
tab = (int*)MALLOC(n*sizeof(int));
546560

547561
rewind(pf);
548-
fgets(line, LINE_SIZE, pf);
562+
if (NULL == fgets(line, LINE_SIZE, pf)) {
563+
line[0] = '\0';
564+
}
549565
fclose(pf);
550566
l = line;
551567
i = 0;

0 commit comments

Comments
 (0)