cngf-pf

continuum model for granular flows with pore-pressure dynamics (renamed from 1d_fd_simple_shear)
git clone git://src.adamsgaard.dk/cngf-pf # fast
git clone https://src.adamsgaard.dk/cngf-pf.git # slow
Log | Files | Refs | README | LICENSE Back to index

commit fb6ea6a60071ea36a485ef60394b247e00918e89
parent 505d04b123cfb85d445042d88900ed96df6d1d4e
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Thu, 22 Apr 2021 12:30:32 +0200

use secure snprintf idiom

Diffstat:
Mcngf-pf.c | 10++++++----
Msimulation.c | 28+++++++++++++++++++---------
2 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/cngf-pf.c b/cngf-pf.c @@ -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; @@ -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 @@ -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; @@ -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", name); + if (ret < 0 || (size_t)ret >= sizeof(message)) + errx(1, "%s: message parsing", __func__); warn_parameter_value(message, value, return_status); } } @@ -711,11 +718,14 @@ implicit_1d_jacobian_poisson_solver(struct simulation *sim, 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);