From a9b89327746b7c5fbe0cedd8aee992de6580ca8a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thomas=20Wei=C3=9Fschuh?= Date: Thu, 12 Jan 2023 00:49:35 +0000 Subject: [PATCH] c.h: avoid undefined behavior in SINT_MAX macro The previous implementation relied on signed-integer overflow. This is undefined behavior. Instead use an implementation that only requires twos-complement representation. This is what everybody uses anyways and it will be required by C23. --- include/c.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/c.h b/include/c.h index 0663774d22..eab6ff5054 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) (((size_t) 1 << (sizeof(t) * 8 - 1)) - 1) +#define SINT_MAX(t) ((t)((~(t) 0) ^ (t) 1 << (sizeof(t) * 8 - 1))) #endif /* UTIL_LINUX_C_H */ -- 2.47.3