@@ -481,6 +481,11 @@ void mcdisplay_mesh_function(struct lines_to_draw *lines_to_draw_output,int inde
481481
482482 Coords *list_startpoints = (Coords*)calloc(n_facets*3,sizeof(Coords));
483483 Coords *list_endpoints = (Coords*)calloc(n_facets*3,sizeof(Coords));
484+ // Check for failed allocation
485+ if (list_startpoints == NULL || list_endpoints == NULL){
486+ free(list_startpoints);
487+ free(list_endpoints);
488+ }
484489
485490 int counter=0;
486491 // For every triangle it should add three lines
@@ -961,9 +966,23 @@ for (int i = 0; i<n_verts; i++){//TODO: Dont use x y z as this doesnt align with
961966// Make lists that will be used to calculate the number of edges
962967int** vert_pairs = (int **)malloc(sizeof(int *)*3*n_faces);
963968int** unique_verts = (int **)malloc(sizeof(int *)*3*n_faces);
969+ // Check for failing mallocs
970+ if ( vert_pairs == NULL || unique_verts == NULL) {
971+ free(vert_pairs);
972+ free(unique_verts);
973+ printf("ERROR: malloc failed on vert pairs or unique verts");
974+ exit(1);
975+ }
964976for (int i=0; i<3*n_faces;i++){
965977 vert_pairs[i] = (int *)malloc(sizeof(int)*2);
966978 unique_verts[i] = (int *)malloc(sizeof(int)*2);
979+ if (vert_pairs[i]==NULL || unique_verts[i] == NULL){
980+ // Should probably be a loop over all vert pairs
981+ free(vert_pairs[i]);
982+ free(unique_verts[i]);
983+ printf("\nERROR: malloc failed on specific vert pairs or unique verts");
984+ exit(1);
985+ }
967986}
968987
969988generate_vertex_vertex_pair_list(faces, vert_pairs, n_faces);
0 commit comments