tfix error handling and use secure snprintf idiom - granular - granular dynamics simulation
 (HTM) git clone git://src.adamsgaard.dk/granular
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit f457ddb46d693870cf3f73a76057f606e94415ec
 (DIR) parent 40ba3713f2f936b47272087219ed0043b7baa995
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Thu, 22 Apr 2021 12:33:46 +0200
       
       fix error handling and use secure snprintf idiom
       
       Diffstat:
         M arrays.c                            |       4 ++--
         M granular.c                          |       9 ++++++---
         M util.c                              |      24 +++++++++++++-----------
       
       3 files changed, 21 insertions(+), 16 deletions(-)
       ---
 (DIR) diff --git a/arrays.c b/arrays.c
       t@@ -9,8 +9,8 @@ void
        check_magnitude(const char *func_name, int limit, int value)
        {
                if (value < limit)
       -                errx("%s: input size %d is less than %d\n",
       -                        func_name, value, limit);
       +                errx(1, "%s: input size %d is less than %d\n",
       +                     func_name, value, limit);
        }
        
        /* Translate a i,j,k index in grid with dimensions nx, ny, nz into a
 (DIR) diff --git a/granular.c b/granular.c
       t@@ -22,6 +22,7 @@ usage(void)
        int
        main(int argc, char *argv[])
        {
       +        int ret;
                struct simulation sim = sim_new();
        
        #ifdef __OpenBSD__
       t@@ -54,9 +55,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: sim.name snprintf", __func__);
       +        } else if (argc > 1)
                        usage();
        
                sim_read_grains(&sim, stdin);
 (DIR) diff --git a/util.c b/util.c
       t@@ -23,12 +23,14 @@ check_float(const char name[], const double value, int *status)
        
                if (isnan(value)) {
                        ret = snprintf(message, sizeof(message), "%s is NaN", name);
       -                if (ret < 0 || ret >= sizeof(buffer))
       -                        err("%s: message parsing", __func__);
       +                if (ret < 0 || (size_t)ret >= sizeof(message))
       +                        errx(1, "%s: message parsing", __func__);
                        warn_parameter_value(message, value, status);
                        *status = 1;
                } else if (isinf(value)) {
       -                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, status);
                        *status = 1;
                }
       t@@ -43,8 +45,8 @@ check_float_non_negative(const char name[], const double value, int *status)
                check_float(name, value, status);
                if (value < 0.0) {
                        ret = snprintf(message, sizeof(message), "%s is negative", name);
       -                if (ret < 0 || ret >= sizeof(buffer))
       -                        err("%s: message parsing", __func__);
       +                if (ret < 0 || (size_t)ret >= sizeof(message))
       +                        errx(1, "%s: message parsing", __func__);
                        warn_parameter_value(message, value, status);
                        *status = 1;
                }
       t@@ -59,8 +61,8 @@ check_float_positive(const char name[], const double value, int *status)
                check_float(name, value, status);
                if (value <= 0.0) {
                        ret = snprintf(message, sizeof(message), "%s is not positive", name);
       -                if (ret < 0 || ret >= sizeof(buffer))
       -                        err("%s: message parsing", __func__);
       +                if (ret < 0 || (size_t)ret >= sizeof(message))
       +                        errx(1, "%s: message parsing", __func__);
                        warn_parameter_value(message, value, status);
                        *status = 1;
                }
       t@@ -74,8 +76,8 @@ check_int_bool(const char name[], const int value, int *status)
        
                if (value < 0 || value > 1) {
                        ret = snprintf(message, sizeof(message), "%s is not 0 or 1", name);
       -                if (ret < 0 || ret >= sizeof(buffer))
       -                        err("%s: message parsing", __func__);
       +                if (ret < 0 || (size_t)ret >= sizeof(message))
       +                        errx(1, "%s: message parsing", __func__);
                        warn_parameter_value(message, (double)value, status);
                        *status = 1;
                }
       t@@ -89,8 +91,8 @@ check_int_non_negative(const char name[], const int value, int *status)
        
                if (value < 0) {
                        ret = snprintf(message, sizeof(message), "%s is negative", name);
       -                if (ret < 0 || ret >= sizeof(buffer))
       -                        err("%s: message parsing", __func__);
       +                if (ret < 0 || (size_t)ret >= sizeof(message))
       +                        errx(1, "%s: message parsing", __func__);
                        warn_parameter_value(message, (double)value, status);
                        *status = 1;
                }