From c123302699919b0f34de9bc5d9019c2c1f14b7a1 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Tue, 21 Sep 2021 00:53:26 -0400 Subject: [PATCH] Use (size_t)-1 instead of SIZE_MAX. We do not require the special characteristics of SIZE_MAX (that it be suitable for use in #if directives), and older library implementations do not define it. Outside of the preprocessor, (size_t)-1 is exactly equivalent so let's just use that instead. --- src/error.c | 4 ++-- src/lbximg.c | 2 +- src/pnm.c | 2 +- src/tools.c | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/error.c b/src/error.c index e46c28f..8071547 100644 --- a/src/error.c +++ b/src/error.c @@ -50,9 +50,9 @@ int lbx_error_new(const char *str) const char **new; size_t size; - if (user_error_max >= MIN(SIZE_MAX, INT_MAX)/2 - LBX_EUBASE) + if (user_error_max >= MIN((size_t)-1, INT_MAX)/2 - LBX_EUBASE) return -1; - if (2 * user_error_max >= SIZE_MAX / sizeof *user_errors) + if (2 * user_error_max >= (size_t)-1 / sizeof *user_errors) return -1; size = 2 * user_error_max * sizeof *user_errors; diff --git a/src/lbximg.c b/src/lbximg.c index 80ffea5..f997506 100644 --- a/src/lbximg.c +++ b/src/lbximg.c @@ -376,7 +376,7 @@ decode(struct lbx_image *img, FILE *palf, FILE *override, int fmt, char **argv) assert(fmt >= 0 && fmt < sizeof formats / sizeof formats[0]); npixels = img->width; - if (img->height && npixels >= SIZE_MAX / img->height) { + if (img->height && npixels >= (size_t)-1 / img->height) { tool_err(-1, "image too large"); goto err; } diff --git a/src/pnm.c b/src/pnm.c index ace5e18..de9d280 100644 --- a/src/pnm.c +++ b/src/pnm.c @@ -154,7 +154,7 @@ static int fprintf_ascii(FILE *f, char *fmt, ...) if (rc < 0) return -1; - assert(rc < SIZE_MAX); + assert(rc < (size_t)-1); buf = malloc(rc+1u); if (!buf) return -1; diff --git a/src/tools.c b/src/tools.c index 200cf9c..580580c 100644 --- a/src/tools.c +++ b/src/tools.c @@ -56,8 +56,8 @@ const char *tool_invocation(void) /* Saturating addition. */ static size_t add_size(size_t a, size_t b) { - if (a >= SIZE_MAX - b) - return SIZE_MAX; + if (a >= (size_t)-1 - b) + return (size_t)-1; return a + b; } @@ -81,7 +81,7 @@ static int vfmsg_internal(FILE *f, int err, const char *fmt, va_list ap) totlen = add_size(totlen, errlen); } - if (totlen == SIZE_MAX || totlen > INT_MAX) + if (totlen == (size_t)-1 || totlen > INT_MAX) return -1; newfmt = malloc(totlen); -- 2.43.2