Skip to content

Commit 64bee5c

Browse files
committed
Add null checks in case of bad allocation
1 parent 30b36e2 commit 64bee5c

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

mcstas-comps/union/Union_logger_2DQ.comp

Lines changed: 6 additions & 0 deletions
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
}

mcstas-comps/union/Union_logger_2D_space.comp

Lines changed: 6 additions & 0 deletions
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
}

mcstas-comps/union/Union_logger_3D_space.comp

Lines changed: 14 additions & 0 deletions
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;

0 commit comments

Comments
 (0)