Skip to content

Commit 24890f6

Browse files
committed
Fix realloc warning upon
1 parent bef5c1d commit 24890f6

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

mcstas-comps/share/union-lib.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,15 +2299,28 @@ struct lines_to_draw draw_line_with_highest_priority(Coords position1,Coords pos
22992299
// Todo: switch to nicer intersect function call
23002300
double *double_dummy = malloc(2*sizeof(double));
23012301
int int_dummy[2];
2302-
2302+
// We need a storing pointer for the reallocs, to ensure that on realloc fail
2303+
// All is handled correctly
2304+
double *tmp;
23032305

23042306
// Find intersections
23052307
for (volume_index = 1;volume_index < number_of_volumes; volume_index++) {
23062308
if (volume_index != N) {
23072309
if (Geometries[volume_index]->eShape==mesh){
2308-
double_dummy = realloc(double_dummy, sizeof(double)*1000);
2309-
temp_intersection = realloc(temp_intersection, sizeof(double)*1000);
2310-
2310+
tmp = realloc(double_dummy, sizeof(double)*1000);
2311+
if ( tmp==NULL ) {
2312+
free(tmp);
2313+
printf("\nERROR: Realloc failed on double dummy");
2314+
exit(1);
2315+
} else {
2316+
double_dummy = tmp;
2317+
tmp = realloc(temp_intersection, sizeof(double)*1000);
2318+
if ( tmp == NULL){
2319+
free(tmp);
2320+
printf("\nERROR: Realloc failed on temp intersection");
2321+
exit(1);
2322+
} else{ temp_intersection = tmp;}
2323+
}
23112324
}
23122325
geometry_output = Geometries[volume_index]->intersect_function(temp_intersection, double_dummy, double_dummy, double_dummy, int_dummy,
23132326
&number_of_solutions, r1, direction, Geometries[volume_index]);
@@ -2318,6 +2331,7 @@ struct lines_to_draw draw_line_with_highest_priority(Coords position1,Coords pos
23182331
}
23192332
}
23202333
}
2334+
free(double_dummy);
23212335
free(temp_intersection);
23222336
// Now we have a list of intersection distances between r1 and r2 and all volumes.
23232337
// This list needs to be sorted before we continue!

0 commit comments

Comments
 (0)