@@ -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 }
@@ -481,6 +489,11 @@ void mcdisplay_mesh_function(struct lines_to_draw *lines_to_draw_output,int inde
481489
482490 Coords *list_startpoints = (Coords*)calloc(n_facets*3,sizeof(Coords));
483491 Coords *list_endpoints = (Coords*)calloc(n_facets*3,sizeof(Coords));
492+ // Check for failed allocation
493+ if (list_startpoints == NULL || list_endpoints == NULL){
494+ free(list_startpoints);
495+ free(list_endpoints);
496+ }
484497
485498 int counter=0;
486499 // For every triangle it should add three lines
@@ -916,8 +929,12 @@ if (mask_string &&
916929// We assume that the input file is using triangular faces, otherwise
917930// the check for closed mesh using the Euler-Poincare characteristic
918931// is wrong.
919- int n_verts, n_faces, n_edges;
932+ int n_verts = 0;
933+ int n_faces = 0;
934+ int n_edges = 0;
920935Coords* verts;
936+ Coords tmp_vert = coords_set(0,0,0);
937+ verts = &tmp_vert; // This tmp vert is just for linter play...
921938int** faces;
922939int unique_index = 0;
923940
@@ -952,7 +969,7 @@ if(strcmp(dot, ".stl") == 0 || strcmp(dot, ".STL") == 0){
952969 exit(1);
953970}
954971// Loop over all vertices and multiply their positions with coordinate_scale
955- for (int i = 0; i<n_verts; i++){//TODO: Dont use x y z as this doesnt align with particle struct.
972+ for (int i = 0; i<n_verts; i++){
956973 verts[i] = coords_scale(verts[i], coordinate_scale);
957974}
958975
@@ -961,9 +978,23 @@ for (int i = 0; i<n_verts; i++){//TODO: Dont use x y z as this doesnt align with
961978// Make lists that will be used to calculate the number of edges
962979int** vert_pairs = (int **)malloc(sizeof(int *)*3*n_faces);
963980int** unique_verts = (int **)malloc(sizeof(int *)*3*n_faces);
981+ // Check for failing mallocs
982+ if ( vert_pairs == NULL || unique_verts == NULL) {
983+ free(vert_pairs);
984+ free(unique_verts);
985+ printf("ERROR: malloc failed on vert pairs or unique verts");
986+ exit(1);
987+ }
964988for (int i=0; i<3*n_faces;i++){
965989 vert_pairs[i] = (int *)malloc(sizeof(int)*2);
966990 unique_verts[i] = (int *)malloc(sizeof(int)*2);
991+ if (vert_pairs[i]==NULL || unique_verts[i] == NULL){
992+ // Should probably be a loop over all vert pairs
993+ free(vert_pairs[i]);
994+ free(unique_verts[i]);
995+ printf("\nERROR: malloc failed on specific vert pairs or unique verts");
996+ exit(1);
997+ }
967998}
968999
9691000generate_vertex_vertex_pair_list(faces, vert_pairs, n_faces);
@@ -989,6 +1020,10 @@ if (mesh_is_not_closed(n_faces,n_verts,n_edges) && !skip_convex_check){
9891020// Calculate bounding box
9901021double max_dist = 0;
9911022double dist = 0;
1023+ if (n_verts <= 0 ){
1024+ printf("\nERROR: Number of vertices read is 0\n");
1025+ exit(1);
1026+ }
9921027Coords B_sphere_x = verts[0];
9931028Coords B_sphere_y = B_sphere_x;
9941029
0 commit comments