Skip to content

Commit f4af281

Browse files
authored
Merge pull request #2249 from mccode-dev/off_file_mesh_component
Mesh component linter fixes
2 parents 18f6ac4 + 7acb119 commit f4af281

File tree

6 files changed

+87
-12
lines changed

6 files changed

+87
-12
lines changed

mcstas-comps/share/union-lib.c

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

23152317
// Find intersections
23162318
for (volume_index = 1;volume_index < number_of_volumes; volume_index++) {
23172319
if (volume_index != N) {
23182320
if (Geometries[volume_index]->eShape==mesh){
2319-
double_dummy = realloc(double_dummy, sizeof(double)*1000);
2320-
temp_intersection = realloc(temp_intersection, sizeof(double)*1000);
2321-
2321+
tmp = realloc(double_dummy, sizeof(double)*1000);
2322+
if ( tmp==NULL ) {
2323+
free(tmp);
2324+
printf("\nERROR: Realloc failed on double dummy");
2325+
exit(1);
2326+
} else {
2327+
double_dummy = tmp;
2328+
tmp = realloc(temp_intersection, sizeof(double)*1000);
2329+
if ( tmp == NULL){
2330+
free(tmp);
2331+
printf("\nERROR: Realloc failed on temp intersection");
2332+
exit(1);
2333+
} else{ temp_intersection = tmp;}
2334+
}
23222335
}
23232336
geometry_output = Geometries[volume_index]->intersect_function(temp_intersection, double_dummy, double_dummy, double_dummy, int_dummy,
23242337
&number_of_solutions, r1, direction, Geometries[volume_index]);
@@ -2329,6 +2342,7 @@ struct lines_to_draw draw_line_with_highest_priority(Coords position1,Coords pos
23292342
}
23302343
}
23312344
}
2345+
free(double_dummy);
23322346
free(temp_intersection);
23332347
// Now we have a list of intersection distances between r1 and r2 and all volumes.
23342348
// This list needs to be sorted before we continue!

mcstas-comps/union/Union_logger_1D.comp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ INITIALIZE
452452
sprintf(this_storage.Detector_1D.Filename,"%s",filename);
453453

454454
// detect variable type and set axis accordingly
455-
int variable_identifier; // 1 time, 2 |q|, ...
455+
int variable_identifier = 0; // 1 time, 2 |q|, ... set to 0 as placeholder to make linter play nice
456456
if (strcmp(variable,"time") == 0) {
457457
variable_identifier = 1;
458458
sprintf(this_storage.Detector_1D.string_axis,"time [s]");

mcstas-comps/union/Union_logger_2DQ.comp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@ double** allocate2Ddouble(int count_x, int count_y) {
397397

398398
// create array or pointers to first elem in each 2D row
399399
double **ptr_array = malloc(sizeof(double*) * count_x);
400+
if (data == NULL || ptr_array == NULL){
401+
free(data);
402+
free(ptr_array);
403+
printf("\nERROR: ptr array or data not allocated in Union_logger_2DQ\n");
404+
exit(1);
405+
}
400406
for (i = 0; i < count_x; i++) {
401407
ptr_array[i] = data + (i*count_y);
402408
}
@@ -509,7 +515,7 @@ INITIALIZE
509515
printf("ERROR, Union logger 2DQ named \"%s\" has an invalid Q_direction_1 string, it should be \"x\",\"y\" or \"z\".\n",NAME_CURRENT_COMP);
510516
exit(1);
511517
}
512-
char temp_string[2];
518+
char temp_string[2] = "UN"; // UN stands for unset. Courtesy of linter play
513519
if (strcmp(Q_direction_2,"x") == 0 || strcmp(Q_direction_2,"X") == 0) {
514520
this_storage.dim_2_choice = 0;
515521
sprintf(this_storage.Detector_2D.string_axis_2,"Q_x [A^-1]");

mcstas-comps/union/Union_logger_2D_space.comp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ double** allocate2Ddouble_2DS(int count_x, int count_y) {
436436

437437
// create array or pointers to first elem in each 2D row
438438
double **ptr_array = malloc(sizeof(double*) * count_x);
439+
if (data == NULL || ptr_array == NULL){
440+
free(data);
441+
free(ptr_array);
442+
printf("\nERROR: ptr array or data not allocated in Union_logger_2D_space\n");
443+
exit(1);
444+
}
439445
for (i = 0; i < count_x; i++) {
440446
ptr_array[i] = data + (i*count_y);
441447
}
@@ -550,7 +556,7 @@ INITIALIZE
550556
exit(1);
551557
}
552558

553-
char temp_string[2];
559+
char temp_string[2] = "UN"; // UN stands for unset. Courtesy of linter play
554560
if (strcmp(D_direction_2,"x") == 0 || strcmp(D_direction_2,"X") == 0) {
555561
this_storage.dim_2_choice = 0;
556562
sprintf(this_storage.Detector_2D.string_axis_2,"x [m]");

mcstas-comps/union/Union_logger_3D_space.comp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,26 @@ void manual_linking_function_logger_processes(char *input_string, struct physics
457457
double*** allocate3Ddouble_3DS(int count_x, int count_y, int count_z, double *storage) {
458458
//double *storage = malloc(count_x * count_y * count_z * sizeof(double));
459459
storage = malloc(count_x * count_y * count_z * sizeof(double));
460+
if (storage == NULL){
461+
free(storage);
462+
printf("\nERROR: storage array not allocated in Union_logger_3D_space\n");
463+
}
460464
double *alloc = storage;
461465
double ***x;
462466
int i,j;
463467
x = malloc(count_x * sizeof(*x));
468+
if ( x == NULL){
469+
free(x);
470+
printf("\nERROR: x array not allocated in Union_logger_3D_space\n");
471+
exit(1);
472+
}
464473
for (i = 0; i < count_x; i++) {
465474
x[i] = malloc(count_y * sizeof(**x));
475+
if ( x[i] == NULL){
476+
free(x[i]);
477+
printf("\nERROR: x[i] array not allocated in Union_logger_3D_space\n");
478+
exit(1);
479+
}
466480
for (j = 0; j < count_y; j++) {
467481
x[i][j] = alloc;
468482
alloc += count_z;
@@ -628,7 +642,7 @@ INITIALIZE
628642
exit(1);
629643
}
630644

631-
char temp_string[2];
645+
char temp_string[2] = "UN"; // UN stands for unset, courtesy of linter play
632646
if (strcmp(D_direction_2,"x") == 0 || strcmp(D_direction_2,"X") == 0) {
633647
this_storage.dim_2_choice = 0;
634648
sprintf(this_storage.Detector_3D.string_axis_2,"x [m]");

mcstas-comps/union/Union_mesh.comp

Lines changed: 39 additions & 4 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
}
@@ -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;
920935
Coords* verts;
936+
Coords tmp_vert = coords_set(0,0,0);
937+
verts = &tmp_vert; // This tmp vert is just for linter play...
921938
int** faces;
922939
int 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
962979
int** vert_pairs = (int **)malloc(sizeof(int *)*3*n_faces);
963980
int** 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+
}
964988
for (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

9691000
generate_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
9901021
double max_dist = 0;
9911022
double dist = 0;
1023+
if (n_verts <= 0 ){
1024+
printf("\nERROR: Number of vertices read is 0\n");
1025+
exit(1);
1026+
}
9921027
Coords B_sphere_x = verts[0];
9931028
Coords B_sphere_y = B_sphere_x;
9941029

0 commit comments

Comments
 (0)