]> granicus.if.org Git - libatomic_ops/commitdiff
Fix SIZET_SAT_ADD for the case of size_t is signed (SunOS 4.X)
authorIvan Maidanski <ivmai@mail.ru>
Fri, 16 Sep 2016 17:45:32 +0000 (20:45 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Fri, 16 Sep 2016 18:03:21 +0000 (21:03 +0300)
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

index 7178f498be125d18bfdbbad1072358e70704fe66..6d4d81a6362b7089bfd85008cc87b9d94e2cae8c 100644 (file)
@@ -135,9 +135,19 @@ static char *get_mmaped(size_t sz)
   return result;
 }
 
+#ifndef SIZE_MAX
+# include <limits.h>
+#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.                          */