Skip to content

Commit

Permalink
fix compiler warnings (#150)
Browse files Browse the repository at this point in the history
* Fixed class_realloc issues, and simplified the macro

* Removed unused parameters

* Removed superflous brackets

* Replaced sprintf (unsafe) with snprintf, making sure to calculate the size correctly of each string, and making it robust for uses in single-line if else satements

* Removed unused variable

* Removed unneccessary define that remained from previous ideas about snprintf

* Fix evolver prototype

* abs vs fabs fixes

* Removed remaining compiler warnings.

* changed version number to 3.2.3

---------

Co-authored-by: schoeneberg <[email protected]>
Co-authored-by: Thomas Tram <[email protected]>
Co-authored-by: lesgourg <[email protected]>
  • Loading branch information
4 people authored Feb 20, 2024
1 parent adafaec commit dcb8be0
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 339 deletions.
27 changes: 24 additions & 3 deletions include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef __COMMON__
#define __COMMON__

#define _VERSION_ "v3.2.2"
#define _VERSION_ "v3.2.3"

/* @cond INCLUDE_WITH_DOXYGEN */

Expand Down Expand Up @@ -100,6 +100,16 @@ extern "C" {

/* general CLASS macros */

//This macro receives additional 'do {' and '} while(0)' to safeguard
//in single-line if else clauses without '{' and '}'
//Also, careful: Since sprintf(NULL,0,x) returns the size of characters
//that are inside of the string x, then the buffer needs to be
//actually one character longer to hold also the null character '\0'
#define class_sprintf(string, format...) do { \
int _buffer_size_sprintf = snprintf(NULL, 0, format); \
snprintf(string, _buffer_size_sprintf+1, format); \
} while (0)

#define class_build_error_string(dest,tmpl,...) { \
ErrorMsg FMsg; \
class_protect_sprintf(FMsg,tmpl,__VA_ARGS__); \
Expand Down Expand Up @@ -188,8 +198,8 @@ extern "C" {
}

/* macro for re-allocating memory, returning error if it failed */
#define class_realloc(pointer, newname, size, error_message_output) { \
pointer=(__typeof__(pointer))realloc(newname,size); \
#define class_realloc(pointer, size, error_message_output) { \
pointer=(__typeof__(pointer))realloc(pointer,size); \
if (pointer == NULL) { \
int size_int; \
size_int = size; \
Expand Down Expand Up @@ -335,6 +345,17 @@ extern "C" {
#define class_print_species(name,type) \
printf("-> %-30s Omega = %-15g , omega = %-15g\n",name,pba->Omega0_##type,pba->Omega0_##type*pba->h*pba->h);

//Generic evolver prototype
#define EVOLVER_PROTOTYPE \
int (*)(double, double *, double *, void *, ErrorMsg), \
double, double, double *, int *, \
int, void *, double, double, \
int (*)(double, void *, double *, ErrorMsg), \
double, double *, int, \
int (*)(double, double *, double *, int, void *, ErrorMsg), \
int (*)(double, double *, double *, void *, ErrorMsg), \
ErrorMsg

/* Forward-Declare the structs of CLASS */
struct background;
struct thermodynamics;
Expand Down
2 changes: 1 addition & 1 deletion include/parallel.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ class TaskSystem {

const unsigned int count_;
std::vector<std::thread> threads_;
std::vector<NotificationQueue> queues_;
std::atomic<unsigned int> index_;
std::vector<NotificationQueue> queues_;
};

}
Expand Down
19 changes: 9 additions & 10 deletions source/background.c
Original file line number Diff line number Diff line change
Expand Up @@ -1442,9 +1442,8 @@ int background_ncdm_init(
pba->error_message),
pba->error_message,
pba->error_message);
pba->q_ncdm[k]=realloc(pba->q_ncdm[k],pba->q_size_ncdm[k]*sizeof(double));
pba->w_ncdm[k]=realloc(pba->w_ncdm[k],pba->q_size_ncdm[k]*sizeof(double));

class_realloc(pba->q_ncdm[k],pba->q_size_ncdm[k]*sizeof(double), pba->error_message);
class_realloc(pba->w_ncdm[k],pba->q_size_ncdm[k]*sizeof(double), pba->error_message);

if (pba->background_verbose > 0) {
printf("ncdm species i=%d sampled with %d points for purpose of perturbation integration\n",
Expand All @@ -1470,8 +1469,8 @@ int background_ncdm_init(
pba->error_message,
pba->error_message);

pba->q_ncdm_bg[k]=realloc(pba->q_ncdm_bg[k],pba->q_size_ncdm_bg[k]*sizeof(double));
pba->w_ncdm_bg[k]=realloc(pba->w_ncdm_bg[k],pba->q_size_ncdm_bg[k]*sizeof(double));
class_realloc(pba->q_ncdm_bg[k],pba->q_size_ncdm_bg[k]*sizeof(double), pba->error_message);
class_realloc(pba->w_ncdm_bg[k],pba->q_size_ncdm_bg[k]*sizeof(double), pba->error_message);

/** - in verbose mode, inform user of number of sampled momenta
for background quantities */
Expand Down Expand Up @@ -1886,9 +1885,9 @@ int background_solve(
double conformal_distance;

/* evolvers */
extern int evolver_rk();
extern int evolver_ndf15();
int (*generic_evolver)() = evolver_ndf15;
extern int evolver_rk(EVOLVER_PROTOTYPE);
extern int evolver_ndf15(EVOLVER_PROTOTYPE);
int (*generic_evolver)(EVOLVER_PROTOTYPE) = evolver_ndf15;

/* initial and final loga values */
double loga_ini, loga_final;
Expand Down Expand Up @@ -2440,9 +2439,9 @@ int background_output_titles(
class_store_columntitle(titles,"(.)rho_idm",pba->has_idm);
if (pba->has_ncdm == _TRUE_) {
for (n=0; n<pba->N_ncdm; n++) {
sprintf(tmp,"(.)rho_ncdm[%d]",n);
class_sprintf(tmp,"(.)rho_ncdm[%d]",n);
class_store_columntitle(titles,tmp,_TRUE_);
sprintf(tmp,"(.)p_ncdm[%d]",n);
class_sprintf(tmp,"(.)p_ncdm[%d]",n);
class_store_columntitle(titles,tmp,_TRUE_);
}
}
Expand Down
30 changes: 15 additions & 15 deletions source/distortions.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ int distortions_constants(struct precision * ppr,
pow(pba->Omega0_b*pow(pba->h,2.)/0.02225,-2./5.)*
pow(pba->T_cmb/2.726,1./5.);

sprintf(psd->sd_PCA_file_generator,"%s/%s",ppr->sd_external_path,"generate_PCA_files.py");
sprintf(psd->sd_detector_list_file,"%s/%s",ppr->sd_external_path,"detectors_list.dat");
class_sprintf(psd->sd_PCA_file_generator,"%s/%s",ppr->sd_external_path,"generate_PCA_files.py");
class_sprintf(psd->sd_detector_list_file,"%s/%s",ppr->sd_external_path,"detectors_list.dat");

return _SUCCESS_;
}
Expand Down Expand Up @@ -223,12 +223,12 @@ int distortions_set_detector(struct precision * ppr,
else {
/* Generate a custom name for this custom detector, so we can check if it has already been defined */
if (psd->has_detector_file == _TRUE_) {
sprintf(psd->sd_detector_name,
class_sprintf(psd->sd_detector_name,
"Custom__%.80s__Detector",
psd->sd_detector_file_name);
}
else {
sprintf(psd->sd_detector_name,
class_sprintf(psd->sd_detector_name,
"Custom__%7.2e_%7.2e_%7.2e_%i_%7.2e__Detector",
psd->sd_detector_nu_min,psd->sd_detector_nu_max,psd->sd_detector_nu_delta,psd->sd_detector_bin_number,psd->sd_detector_delta_Ic);
}
Expand Down Expand Up @@ -282,7 +282,7 @@ int distortions_set_detector(struct precision * ppr,
psd->error_message,
"Detector property type disagrees between stored detector '%s' and input -> Userdefined (input) vs Noisefile (stored)",
detector_name);
sprintf(psd->sd_detector_file_name, "%s", detector_noise_file_name);
class_sprintf(psd->sd_detector_file_name, "%s", detector_noise_file_name);
psd->has_detector_file = has_detector_noise_file;
}
else {
Expand All @@ -304,7 +304,7 @@ int distortions_set_detector(struct precision * ppr,
psd->error_message,
"Delta frequency (sd_detector_nu_delta) disagrees between stored detector '%s' and input -> %.10e (input) vs %.10e (stored)",
detector_name,psd->sd_detector_nu_delta,nu_delta);
class_test(fabs(psd->sd_detector_bin_number-N_bins)>ppr->tol_sd_detector,
class_test(abs(psd->sd_detector_bin_number-N_bins)>ppr->tol_sd_detector,
psd->error_message,
"Number of bins (sd_detector_bin_number) disagrees between stored detector '%s' and input -> %i (input) vs %i (stored)",
detector_name,psd->sd_detector_bin_number,N_bins);
Expand Down Expand Up @@ -391,7 +391,7 @@ int distortions_generate_detector(struct precision * ppr,
}

if (psd->has_detector_file == _TRUE_) {
sprintf(temporary_string,"python %s %s %s %s %.10e %.10e %i %i %.10e %.10e %.10e",
class_sprintf(temporary_string,"python %s %s %s %s %.10e %.10e %i %i %.10e %.10e %.10e",
psd->sd_PCA_file_generator,
psd->sd_detector_name,
ppr->sd_external_path,
Expand All @@ -406,7 +406,7 @@ int distortions_generate_detector(struct precision * ppr,

}
else {
sprintf(temporary_string,"python %s %s %.10e %.10e %.10e %i %.10e %.10e %i %.10e %i %.10e %.10e %.10e",
class_sprintf(temporary_string,"python %s %s %.10e %.10e %.10e %i %.10e %.10e %i %.10e %i %.10e %.10e %.10e",
psd->sd_PCA_file_generator,
psd->sd_detector_name,
psd->sd_detector_nu_min,
Expand Down Expand Up @@ -452,7 +452,7 @@ int distortions_read_detector_noisefile(struct precision * ppr,
int numcols;

/** Open file */
sprintf(psd->sd_detector_noise_file,"%s/%s",ppr->sd_external_path,psd->sd_detector_file_name);
class_sprintf(psd->sd_detector_noise_file,"%s/%s",ppr->sd_external_path,psd->sd_detector_file_name);
class_open(infile, psd->sd_detector_noise_file, "r", psd->error_message);

/** Read header */
Expand Down Expand Up @@ -1418,7 +1418,7 @@ int distortions_read_br_data(struct precision * ppr,
int headlines = 0;

/** Open file */
sprintf(br_file,"%s/%s_branching_ratios.dat", ppr->sd_external_path, psd->sd_detector_name);
class_sprintf(br_file,"%s/%s_branching_ratios.dat", ppr->sd_external_path, psd->sd_detector_name);
class_open(infile, br_file, "r", psd->error_message);

/** Read header */
Expand Down Expand Up @@ -1651,7 +1651,7 @@ int distortions_read_sd_data(struct precision * ppr,
int index_x,index_k;

/** Open file */
sprintf(sd_file,"%s/%s_distortions_shapes.dat",ppr->sd_external_path, psd->sd_detector_name);
class_sprintf(sd_file,"%s/%s_distortions_shapes.dat",ppr->sd_external_path, psd->sd_detector_name);
class_open(infile, sd_file, "r", psd->error_message);

/** Read header */
Expand Down Expand Up @@ -1931,16 +1931,16 @@ int distortions_output_sd_titles(struct distortions * psd,
class_store_columntitle(titles,"SD_tot",_TRUE_);
for (index_type=0;index_type<psd->type_size;++index_type){
if (index_type==psd->index_type_g) {
sprintf(temp_title,"SD[g]");
class_sprintf(temp_title,"SD[g]");
}
if (index_type==psd->index_type_y) {
sprintf(temp_title,"SD[y]");
class_sprintf(temp_title,"SD[y]");
}
if (index_type==psd->index_type_mu) {
sprintf(temp_title,"SD[mu]");
class_sprintf(temp_title,"SD[mu]");
}
if (index_type>=psd->index_type_PCA) {
sprintf(temp_title,"SD[e_%i]",(index_type-psd->index_type_PCA));
class_sprintf(temp_title,"SD[e_%i]",(index_type-psd->index_type_PCA));
}
class_store_columntitle(titles,temp_title,_TRUE_);
}
Expand Down
Loading

0 comments on commit dcb8be0

Please sign in to comment.