From 23360cc4980213d375718b7c124ecc0a21a1b1b7 Mon Sep 17 00:00:00 2001 From: Stephen Hines Date: Tue, 29 Sep 2015 09:28:41 -0700 Subject: [PATCH] Remove invalid left shifts of -1. Bug: 24492248 Shifting sign bits left is considered undefined behavior, so we need to switch these uses to unsigned equivalents. The time_t-related code is updated relative to upstream sources. Change-Id: I226e5a929a10f5c57dfcb90c748fdac34eb377c2 --- libc/bionic/semaphore.cpp | 2 +- libc/tzcode/private.h | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/libc/bionic/semaphore.cpp b/libc/bionic/semaphore.cpp index 0b046509d..ff84443e1 100644 --- a/libc/bionic/semaphore.cpp +++ b/libc/bionic/semaphore.cpp @@ -80,7 +80,7 @@ static inline int SEMCOUNT_TO_VALUE(unsigned int sval) { #define SEMCOUNT_ONE SEMCOUNT_FROM_VALUE(1) // The value -1 as a sem->count bit-pattern. -#define SEMCOUNT_MINUS_ONE SEMCOUNT_FROM_VALUE(-1) +#define SEMCOUNT_MINUS_ONE SEMCOUNT_FROM_VALUE(~0U) #define SEMCOUNT_DECREMENT(sval) (((sval) - (1U << SEMCOUNT_VALUE_SHIFT)) & SEMCOUNT_VALUE_MASK) #define SEMCOUNT_INCREMENT(sval) (((sval) + (1U << SEMCOUNT_VALUE_SHIFT)) & SEMCOUNT_VALUE_MASK) diff --git a/libc/tzcode/private.h b/libc/tzcode/private.h index c30c71101..494e14249 100644 --- a/libc/tzcode/private.h +++ b/libc/tzcode/private.h @@ -328,15 +328,20 @@ const char * scheck(const char * string, const char * format); #define TYPE_SIGNED(type) (((type) -1) < 0) #endif /* !defined TYPE_SIGNED */ -/* The minimum and maximum finite time values. */ -static time_t const time_t_min = - (TYPE_SIGNED(time_t) - ? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1) - : 0); -static time_t const time_t_max = - (TYPE_SIGNED(time_t) - ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)) - : -1); +#define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0) + + /* Max and min values of the integer type T, of which only the bottom + * B bits are used, and where the highest-order used bit is considered + * to be a sign bit if T is signed. */ +#define MAXVAL(t, b) \ + ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \ + - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t))))) +#define MINVAL(t, b) \ + ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0)) + +/* The minimum and maximum finite time values. This assumes no padding. */ +static time_t const time_t_min = MINVAL(time_t, TYPE_BIT(time_t)); +static time_t const time_t_max = MAXVAL(time_t, TYPE_BIT(time_t)); #ifndef INT_STRLEN_MAXIMUM /* -- 2.11.0