tfix error handling and use secure snprintf idiom - granular - granular dynamic… | |
git clone git://src.adamsgaard.dk/granular | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit f457ddb46d693870cf3f73a76057f606e94415ec | |
parent 40ba3713f2f936b47272087219ed0043b7baa995 | |
Author: Anders Damsgaard <[email protected]> | |
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(-) | |
--- | |
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 | |
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); | |
diff --git a/util.c b/util.c | |
t@@ -23,12 +23,14 @@ check_float(const char name[], const double value, int *st… | |
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", nam… | |
+ 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 val… | |
check_float(name, value, status); | |
if (value < 0.0) { | |
ret = snprintf(message, sizeof(message), "%s is negative", nam… | |
- 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, … | |
check_float(name, value, status); | |
if (value <= 0.0) { | |
ret = snprintf(message, sizeof(message), "%s is not positive",… | |
- 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 *stat… | |
if (value < 0 || value > 1) { | |
ret = snprintf(message, sizeof(message), "%s is not 0 or 1", n… | |
- 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, i… | |
if (value < 0) { | |
ret = snprintf(message, sizeof(message), "%s is negative", nam… | |
- 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; | |
} |