Skip to content

Commit d05cb0f

Browse files
committed
Add reallocs failure grabs, and remove a done todo comment.
1 parent 64bee5c commit d05cb0f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

mcstas-comps/union/Union_mesh.comp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ void read_stl(char* filename,
161161

162162
// Allocate memory for triangles
163163
triangles = (Triangle *)malloc(capacity * sizeof(Triangle));
164+
if (triangles == NULL){
165+
free(triangles);
166+
printf("\nERROR: malloc failed for triangles");
167+
exit(1);
168+
}
164169

165170
Triangle current;
166171

@@ -200,12 +205,15 @@ void read_stl(char* filename,
200205
current.attribute_byte_count = 0; // ASCII STL always 0
201206
if (count >= capacity) {
202207
capacity *= 2;
203-
triangles = realloc(triangles, capacity * sizeof(Triangle));
204-
if (!triangles) {
208+
void * tmp = realloc(triangles, capacity * sizeof(Triangle));
209+
if (tmp) {
210+
free(triangles);
211+
free(tmp);
205212
perror("ERROR: Memory reallocation failed");
206213
fclose(file);
207214
exit(1);
208215
}
216+
triangles = tmp;
209217
}
210218
triangles[count++] = current;
211219
}
@@ -957,7 +965,7 @@ if(strcmp(dot, ".stl") == 0 || strcmp(dot, ".STL") == 0){
957965
exit(1);
958966
}
959967
// Loop over all vertices and multiply their positions with coordinate_scale
960-
for (int i = 0; i<n_verts; i++){//TODO: Dont use x y z as this doesnt align with particle struct.
968+
for (int i = 0; i<n_verts; i++){
961969
verts[i] = coords_scale(verts[i], coordinate_scale);
962970
}
963971

0 commit comments

Comments
 (0)