tuse secure snprintf idiom - cngf-pf - continuum model for granular flows with … | |
git clone git://src.adamsgaard.dk/cngf-pf | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit fb6ea6a60071ea36a485ef60394b247e00918e89 | |
parent 505d04b123cfb85d445042d88900ed96df6d1d4e | |
Author: Anders Damsgaard <[email protected]> | |
Date: Thu, 22 Apr 2021 12:30:32 +0200 | |
use secure snprintf idiom | |
Diffstat: | |
M cngf-pf.c | 10 ++++++---- | |
M simulation.c | 28 +++++++++++++++++++--------- | |
2 files changed, 25 insertions(+), 13 deletions(-) | |
--- | |
diff --git a/cngf-pf.c b/cngf-pf.c | |
t@@ -69,7 +69,7 @@ usage(void) | |
int | |
main(int argc, char *argv[]) | |
{ | |
- int i, normalize, dt_override; | |
+ int i, normalize, dt_override, ret; | |
unsigned long iter; | |
double new_phi, new_k, filetimeclock; | |
struct simulation sim; | |
t@@ -228,9 +228,11 @@ main(int argc, char *argv[]) | |
usage(); | |
} ARGEND; | |
- if (argc == 1 && argv[0]) | |
- snprintf(sim.name, sizeof(sim.name), "%s", argv[0]); | |
- else if (argc > 1) | |
+ if (argc == 1 && argv[0]) { | |
+ ret = snprintf(sim.name, sizeof(sim.name), "%s", argv[0]); | |
+ if (ret < 0 || (size_t)ret >= sizeof(sim.name)) | |
+ errx(1, "%s: could not write sim.name", __func__); | |
+ } else if (argc > 1) | |
usage(); | |
if (sim.nz < 1) | |
diff --git a/simulation.c b/simulation.c | |
t@@ -20,7 +20,11 @@ | |
void | |
init_sim(struct simulation *sim) | |
{ | |
- snprintf(sim->name, sizeof(sim->name), DEFAULT_SIMULATION_NAME); | |
+ int ret; | |
+ | |
+ ret = snprintf(sim->name, sizeof(sim->name), DEFAULT_SIMULATION_NAME); | |
+ if (ret < 0 || (size_t)ret == sizeof(sim->name)) | |
+ err(1, "%s: could not write simulation name", __func__); | |
sim->G = 9.81; | |
t@@ -212,18 +216,21 @@ warn_parameter_value(const char message[], | |
static void | |
check_float(const char name[], const double value, int *return_status) | |
{ | |
+ int ret; | |
+ char message[100]; | |
+ | |
#ifdef SHOW_PARAMETERS | |
printf("%30s: %.17g\n", name, value); | |
#endif | |
if (isnan(value)) { | |
- char message[100]; | |
- | |
- snprintf(message, sizeof(message), "%s is NaN", name); | |
+ ret = snprintf(message, sizeof(message), "%s is NaN", name); | |
+ if (ret < 0 || (size_t)ret >= sizeof(message)) | |
+ errx(1, "%s: message parsing", __func__); | |
warn_parameter_value(message, value, return_status); | |
} else if (isinf(value)) { | |
- char message[100]; | |
- | |
- snprintf(message, sizeof(message), "%s is infinite", name); | |
+ ret = snprintf(message, sizeof(message), "%s is infinite", nam… | |
+ if (ret < 0 || (size_t)ret >= sizeof(message)) | |
+ errx(1, "%s: message parsing", __func__); | |
warn_parameter_value(message, value, return_status); | |
} | |
} | |
t@@ -711,11 +718,14 @@ implicit_1d_jacobian_poisson_solver(struct simulation *s… | |
void | |
write_output_file(struct simulation *sim, const int normalize) | |
{ | |
+ int ret; | |
char outfile[200]; | |
FILE *fp; | |
- snprintf(outfile, sizeof(outfile), "%s.output%05d.txt", | |
- sim->name, sim->n_file++); | |
+ ret = snprintf(outfile, sizeof(outfile), "%s.output%05d.txt", | |
+ sim->name, sim->n_file++); | |
+ if (ret < 0 || (size_t)ret >= sizeof(outfile)) | |
+ errx(1, "%s: outfile snprintf", __func__); | |
if ((fp = fopen(outfile, "w")) != NULL) { | |
print_output(sim, fp, normalize); |