diff --git a/qpms/qpms_error.h b/qpms/qpms_error.h index 48111d1..b1ea0f7 100644 --- a/qpms/qpms_error.h +++ b/qpms/qpms_error.h @@ -21,6 +21,8 @@ void qpms_pr_debug_at_flf(const char *filename, unsigned int linenum, #define QPMS_CRASHING_MALLOC(pointer, size) {(pointer) = malloc(size); if(!pointer) qpms_pr_debug_at_flf(__FILE__,__LINE__,__func__, "Allocation of %zd bytes for " #pointer " failed.", (size_t) (size));} +#define QPMS_CRASHING_REALLOC(pointer, size) {(pointer) = realloc(pointer, size); if(!pointer) qpms_pr_debug_at_flf(__FILE__,__LINE__,__func__, "Rellocation of %zd bytes for " #pointer " failed.", (size_t) (size));} + #define QPMS_WTF qpms_pr_error_at_flf(__FILE__,__LINE__,__func__,"Unexpected error.") #define QPMS_ENSURE_SUCCESS(x) {if(x) QPMS_WTF;} diff --git a/qpms/scatsystem.c b/qpms/scatsystem.c index 8721f3f..70a466c 100644 --- a/qpms/scatsystem.c +++ b/qpms/scatsystem.c @@ -748,8 +748,9 @@ complex double *qpms_orbit_irrep_basis(size_t *basis_size, check_norm_compat(bspec); const size_t n = bspec->n; const qpms_gmi_t N = ot->size; - if (target == NULL) - target = malloc(n*n*N*N*sizeof(complex double)); + const bool newtarget = (target == NULL); + if (newtarget) + QPMS_CRASHING_MALLOC(target,n*n*N*N*sizeof(complex double)); if (target == NULL) abort(); memset(target, 0, n*n*N*N*sizeof(complex double)); @@ -786,7 +787,8 @@ complex double *qpms_orbit_irrep_basis(size_t *basis_size, if(s[bs] < SVD_ATOL) break; } - memcpy(target, V_H, bs*N*n*n*sizeof(complex double)); + memcpy(target, V_H, bs*N*n*sizeof(complex double)); + if(newtarget) QPMS_CRASHING_REALLOC(target, bs*N*n*sizeof(complex double)); if(basis_size) *basis_size = bs; free(U);