From: Thomas Weißschuh Date: Sat, 14 Jan 2023 22:16:28 +0000 (+0000) Subject: c.h: really avoid undefined behavior in SINT_MAX X-Git-Tag: v2.39-rc1~162 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e53463ea40c58a6f84a597966ac69be9be72cd7d;p=thirdparty%2Futil-linux.git c.h: really avoid undefined behavior in SINT_MAX The previous fix in #2013 still invoked undefined behavior by shifting into the sign-bit. Now we have a correct, albeit unwieldy solution that avoids undefined behavior. Instead of bit fiddling it uses plain addition and substraction. We are looking for (in LaTeX notation): 2^(n - 1) - 1 = 2 * 2^(n - 2) - 1 = 2^(n - 2) + 2^(n - 2) - 1 = 2^(n - 2) - 1 + 2^(n - 2) --- diff --git a/include/c.h b/include/c.h index eab6ff5054..0f25fb009d 100644 --- a/include/c.h +++ b/include/c.h @@ -526,6 +526,6 @@ static inline void print_features(const char **features, const char *prefix) # define MAP_ANONYMOUS (MAP_ANON) #endif -#define SINT_MAX(t) ((t)((~(t) 0) ^ (t) 1 << (sizeof(t) * 8 - 1))) +#define SINT_MAX(t) ((t) (1 << (sizeof(t) * 8 - 2)) - 1 + ((t) 1 << (sizeof(t) * 8 - 2))) #endif /* UTIL_LINUX_C_H */