From b1365bde2444003ca95bc28ff06bdd34a81839a7 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Fri, 16 Sep 2016 20:45:32 +0300 Subject: Fix SIZET_SAT_ADD for the case of size_t is signed (SunOS 4.X) Negative size_t value is impossible in a correct C implementation, but quite possible under SunOS 4.X. * src/atomic_ops_malloc.c: Include limits.h (unless SIZE_MAX already defined). * src/atomic_ops_malloc.c (AO_SIZE_MAX): New macro. * src/atomic_ops_malloc.c (SIZET_SAT_ADD): Use AO_SIZE_MAX instead of ~(size_t)0. --- src/atomic_ops_malloc.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/atomic_ops_malloc.c b/src/atomic_ops_malloc.c index 7178f49..6d4d81a 100644 --- a/src/atomic_ops_malloc.c +++ b/src/atomic_ops_malloc.c @@ -135,9 +135,19 @@ static char *get_mmaped(size_t sz) return result; } +#ifndef SIZE_MAX +# include +#endif +#ifdef SIZE_MAX +# define AO_SIZE_MAX SIZE_MAX +#else +# define AO_SIZE_MAX (~(size_t)0) +#endif + /* Saturated addition of size_t values. Used to avoid value wrap */ /* around on overflow. The arguments should have no side effects. */ -#define SIZET_SAT_ADD(a, b) ((a) < ~(size_t)(b) ? (a) + (b) : ~(size_t)0) +#define SIZET_SAT_ADD(a, b) \ + ((a) < AO_SIZE_MAX - (b) ? (a) + (b) : AO_SIZE_MAX) /* Allocate an object of size (incl. header) of size > CHUNK_SIZE. */ /* sz includes space for an AO_t-sized header. */ -- cgit v1.2.1