Blame SOURCES/0036-util.h-implement-add-mul-sub-for-more-integer-types.patch

b15ea1
From 3536ccae29c9079eeee99fd363c7dec8a243ab58 Mon Sep 17 00:00:00 2001
b15ea1
From: Peter Jones <pjones@redhat.com>
b15ea1
Date: Mon, 17 Jun 2019 16:37:29 -0400
b15ea1
Subject: [PATCH 36/86] util.h: implement add()/mul()/sub() for more integer
b15ea1
 types.
b15ea1
b15ea1
This adds the following:
b15ea1
uint_add()
b15ea1
uint_mul()
b15ea1
uint_sub()
b15ea1
long_sub()
b15ea1
ulong_sub()
b15ea1
b15ea1
Additionally it renames ulong_mult() to ulong_mul() and long_mult() to
b15ea1
long_mul().
b15ea1
b15ea1
As before, all of these are available without caring about the types,
b15ea1
as if declared:
b15ea1
b15ea1
bool add(TYPE addend, TYPE addend, TYPE *sum);
b15ea1
bool mul(TYPE factor, TYPE factor, TYPE *product);
b15ea1
bool sub(TYPE minuend, TYPE subtractahend, TYPE *difference);
b15ea1
b15ea1
If overflow would occur, the pointer target for the result is not
b15ea1
changed and the function returns true, otherwise it returns false.
b15ea1
b15ea1
Signed-off-by: Peter Jones <pjones@redhat.com>
b15ea1
---
b15ea1
 src/efivar.h   |   1 +
b15ea1
 src/safemath.h | 208 +++++++++++++++++++++++++++++++++++++++++++++++++
b15ea1
 src/util.h     |  96 -----------------------
b15ea1
 3 files changed, 209 insertions(+), 96 deletions(-)
b15ea1
 create mode 100644 src/safemath.h
b15ea1
b15ea1
diff --git a/src/efivar.h b/src/efivar.h
b15ea1
index 3d4b429631e..646863d14c5 100644
b15ea1
--- a/src/efivar.h
b15ea1
+++ b/src/efivar.h
b15ea1
@@ -23,6 +23,7 @@
b15ea1
 #include <efivar/efivar.h>
b15ea1
 
b15ea1
 #include "util.h"
b15ea1
+#include "safemath.h"
b15ea1
 #include "efivar_endian.h"
b15ea1
 #include "lib.h"
b15ea1
 #include "guid.h"
b15ea1
diff --git a/src/safemath.h b/src/safemath.h
b15ea1
new file mode 100644
b15ea1
index 00000000000..08dfef7ec0b
b15ea1
--- /dev/null
b15ea1
+++ b/src/safemath.h
b15ea1
@@ -0,0 +1,208 @@
b15ea1
+/*
b15ea1
+ * safemath.h
b15ea1
+ * Copyright 2016-2019 Peter Jones <pjones@redhat.com>
b15ea1
+ *
b15ea1
+ * This library is free software; you can redistribute it and/or
b15ea1
+ * modify it under the terms of the GNU Lesser General Public License as
b15ea1
+ * published by the Free Software Foundation; either version 2.1 of the
b15ea1
+ * License, or (at your option) any later version.
b15ea1
+ *
b15ea1
+ * This library is distributed in the hope that it will be useful,
b15ea1
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
b15ea1
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b15ea1
+ * Lesser General Public License for more details.
b15ea1
+ *
b15ea1
+ * You should have received a copy of the GNU Lesser General Public
b15ea1
+ * License along with this library; if not, see
b15ea1
+ * <http://www.gnu.org/licenses/>.
b15ea1
+ *
b15ea1
+ */
b15ea1
+
b15ea1
+#ifndef SAFEMATH_H_
b15ea1
+#define SAFEMATH_H_
b15ea1
+
b15ea1
+/*
b15ea1
+ * I'm not actually sure when these appear, but they're present in the
b15ea1
+ * version in front of me.
b15ea1
+ */
b15ea1
+#if defined(__GNUC__) && defined(__GNUC_MINOR__)
b15ea1
+#if __GNUC__ >= 5 && __GNUC_MINOR__ >= 1
b15ea1
+#define int_add(a, b, c) __builtin_add_overflow(a, b, c)
b15ea1
+#define uint_add(a, b, c) __builtin_add_overflow(a, b, c)
b15ea1
+#define long_add(a, b, c) __builtin_add_overflow(a, b, c)
b15ea1
+#define ulong_add(a, b, c) __builtin_add_overflow(a, b, c)
b15ea1
+
b15ea1
+#define int_mul(a, b, c) __builtin_mul_overflow(a, b, c)
b15ea1
+#define uint_mul(a, b, c) __builtin_mul_overflow(a, b, c)
b15ea1
+#define long_mul(a, b, c) __builtin_mul_overflow(a, b, c)
b15ea1
+#define ulong_mul(a, b, c) __builtin_mul_overflow(a, b, c)
b15ea1
+
b15ea1
+#define int_sub(a, b, c) __builtin_sub_overflow(a, b, c)
b15ea1
+#define uint_sub(a, b, c) __builtin_sub_overflow(a, b, c)
b15ea1
+#define long_sub(a, b, c) __builtin_sub_overflow(a, b, c)
b15ea1
+#define ulong_sub(a, b, c) __builtin_sub_overflow(a, b, c)
b15ea1
+#endif
b15ea1
+#endif
b15ea1
+
b15ea1
+#ifndef int_add
b15ea1
+#define int_add(a, b, c) ({					\
b15ea1
+		const int _limit = INT_MAX;			\
b15ea1
+		long int _ret = _limit - (a);			\
b15ea1
+		_ret = _ret > (b);				\
b15ea1
+		if (!_ret)					\
b15ea1
+			*(c) = ((a) + (b));			\
b15ea1
+		(bool)_ret;					\
b15ea1
+	})
b15ea1
+#endif
b15ea1
+
b15ea1
+#ifndef uint_add
b15ea1
+#define uint_add(a, b, c) ({					\
b15ea1
+		const unsigned int _limit = UINT_MAX;		\
b15ea1
+		unsigned int _ret = _limit - (a);		\
b15ea1
+		_ret = _ret > (b);				\
b15ea1
+		if (!_ret)					\
b15ea1
+			*(c) = ((a) + (b));			\
b15ea1
+		(bool)_ret;					\
b15ea1
+	})
b15ea1
+#endif
b15ea1
+
b15ea1
+#ifndef long_add
b15ea1
+#define long_add(a, b, c) ({					\
b15ea1
+		const long _limit = LONG_MAX;			\
b15ea1
+		long _ret = _limit - (a);			\
b15ea1
+		_ret = _ret > (b);				\
b15ea1
+		if (!_ret)					\
b15ea1
+			*(c) = ((a) + (b));			\
b15ea1
+		(bool)_ret;					\
b15ea1
+	})
b15ea1
+#endif
b15ea1
+
b15ea1
+#ifndef ulong_add
b15ea1
+#define ulong_add(a, b, c) ({					\
b15ea1
+		const unsigned long _limit = ULONG_MAX;		\
b15ea1
+		unsigned long _ret = _limit - (a);		\
b15ea1
+		_ret = _ret > (b);				\
b15ea1
+		if (!_ret)					\
b15ea1
+			*(c) = ((a) + (b));			\
b15ea1
+		(bool)_ret;					\
b15ea1
+	})
b15ea1
+#endif
b15ea1
+
b15ea1
+#ifndef int_mul
b15ea1
+#define int_mul(a, b, c) ({						\
b15ea1
+		int _ret;						\
b15ea1
+		_ret = __builtin_popcount(a) + __builtin_popcount(b);	\
b15ea1
+		_ret = _ret < ((sizeof(a) + sizeof(b)) << 4);		\
b15ea1
+		if (!_ret)						\
b15ea1
+			*(c) = ((a) * (b));				\
b15ea1
+		(bool)_ret;						\
b15ea1
+	})
b15ea1
+#endif
b15ea1
+
b15ea1
+#ifndef uint_mul
b15ea1
+#define uint_mul(a, b, c) int_mul(a, b, c)
b15ea1
+#endif
b15ea1
+
b15ea1
+#ifndef long_mul
b15ea1
+#define long_mul(a, b, c) int_mul(a, b, c)
b15ea1
+#endif
b15ea1
+
b15ea1
+#ifndef ulong_mul
b15ea1
+#define ulong_mul(a, b, c) int_mul(a, b, c)
b15ea1
+#endif
b15ea1
+
b15ea1
+#ifndef int_sub
b15ea1
+#define int_sub(a, b, c) ({					\
b15ea1
+		const long _min_limit = INT_MIN;		\
b15ea1
+		const long _max_limit = INT_MAX;		\
b15ea1
+		int _ret;					\
b15ea1
+		_ret = _min_limit + (b);			\
b15ea1
+		_ret = !(_ret < (a));				\
b15ea1
+		if (!_ret) {					\
b15ea1
+			_ret = _max_limit - (a);		\
b15ea1
+			_ret = _ret > (b);			\
b15ea1
+		}						\
b15ea1
+		if (!_ret)					\
b15ea1
+			*(c) = ((a) - (b));			\
b15ea1
+		(bool)_ret;					\
b15ea1
+	})
b15ea1
+#endif
b15ea1
+
b15ea1
+#ifndef uint_sub
b15ea1
+#define uint_sub(a, b, c) ({					\
b15ea1
+		const unsigned int _limit = UINT_MAX;		\
b15ea1
+		unsigned int _ret = _limit - (a);		\
b15ea1
+		_ret = _ret > (b);				\
b15ea1
+		if (!_ret)					\
b15ea1
+			*(c) = ((a) - (b));			\
b15ea1
+		(bool)_ret;					\
b15ea1
+	})
b15ea1
+#endif
b15ea1
+
b15ea1
+#ifndef long_sub
b15ea1
+#define long_sub(a, b, c) ({					\
b15ea1
+		const long _min_limit = LONG_MIN;		\
b15ea1
+		const long _max_limit = LONG_MAX;		\
b15ea1
+		int _ret;					\
b15ea1
+		_ret = _min_limit + (b);			\
b15ea1
+		_ret = !(_ret < (a));				\
b15ea1
+		if (!_ret) {					\
b15ea1
+			_ret = _max_limit - (a);		\
b15ea1
+			_ret = _ret > (b);			\
b15ea1
+		}						\
b15ea1
+		if (!_ret)					\
b15ea1
+			*(c) = ((a) - (b));			\
b15ea1
+		(bool)_ret;					\
b15ea1
+	})
b15ea1
+#endif
b15ea1
+
b15ea1
+#ifndef ulong_sub
b15ea1
+#define ulong_sub(a, b, c) ({					\
b15ea1
+		const unsigned long _limit = ULONG_MAX;		\
b15ea1
+		unsigned long _ret = _limit - (a);		\
b15ea1
+		_ret = _ret > (b);				\
b15ea1
+		if (!_ret)					\
b15ea1
+			*(c) = ((a) - (b));			\
b15ea1
+		_ret;						\
b15ea1
+	})
b15ea1
+#endif
b15ea1
+
b15ea1
+#if defined(__GNUC__) && defined(__GNUC_MINOR__)
b15ea1
+#if __GNUC__ >= 5 && __GNUC_MINOR__ >= 1
b15ea1
+#define add(a, b, c) _Generic((c),					\
b15ea1
+			      int *: int_add(a, b, c),			\
b15ea1
+			      unsigned int *: uint_add(a, b, c),	\
b15ea1
+			      long *: long_add(a, b, c),		\
b15ea1
+			      unsigned long *: ulong_add(a, b, c))
b15ea1
+#define sub(a, b, c) _Generic((c),					\
b15ea1
+			      int *: int_sub(a, b, c),			\
b15ea1
+			      unsigned int *: uint_sub(a, b, c),	\
b15ea1
+			      long *: long_sub(a, b, c),		\
b15ea1
+			      unsigned long *: ulong_sub(a, b, c))
b15ea1
+#define mul(a, b, c) _Generic((c),					\
b15ea1
+			      int *: int_sub(a, b, c),			\
b15ea1
+			      unsigned int *: uint_mul(a, b, c),	\
b15ea1
+			      long *: long_mul(a, b, c),		\
b15ea1
+			      unsigned long *: ulong_mul(a, b, c))
b15ea1
+#endif
b15ea1
+#endif
b15ea1
+
b15ea1
+#ifndef add
b15ea1
+#define add(a, b, c) ({						\
b15ea1
+		(*(c)) = ((a) + (b));				\
b15ea1
+		})
b15ea1
+#endif
b15ea1
+#ifndef mul
b15ea1
+#define mul(a, b, c) ({						\
b15ea1
+		(*(c)) = ((a) * (b));				\
b15ea1
+		})
b15ea1
+#endif
b15ea1
+#ifndef sub
b15ea1
+#define sub(a, b, c) ({						\
b15ea1
+		(*(c)) = ((a) - (b));				\
b15ea1
+		})
b15ea1
+#endif
b15ea1
+
b15ea1
+
b15ea1
+#endif /* !SAFEMATH_H_ */
b15ea1
+// vim:fenc=utf-8:tw=75:noet
b15ea1
diff --git a/src/util.h b/src/util.h
b15ea1
index 712abea2d42..3f68d812700 100644
b15ea1
--- a/src/util.h
b15ea1
+++ b/src/util.h
b15ea1
@@ -61,102 +61,6 @@
b15ea1
 #define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x)))
b15ea1
 #endif
b15ea1
 
b15ea1
-/*
b15ea1
- * I'm not actually sure when these appear, but they're present in the
b15ea1
- * version in front of me.
b15ea1
- */
b15ea1
-#if defined(__GNUC__) && defined(__GNUC_MINOR__)
b15ea1
-#if __GNUC__ >= 5 && __GNUC_MINOR__ >= 1
b15ea1
-#define int_add(a, b, c) __builtin_add_overflow(a, b, c)
b15ea1
-#define long_add(a, b, c) __builtin_add_overflow(a, b, c)
b15ea1
-#define long_mult(a, b, c) __builtin_mul_overflow(a, b, c)
b15ea1
-#define ulong_add(a, b, c) __builtin_add_overflow(a, b, c)
b15ea1
-#define ulong_mult(a, b, c) __builtin_mul_overflow(a, b, c)
b15ea1
-#endif
b15ea1
-#endif
b15ea1
-#ifndef int_add
b15ea1
-#define int_add(a, b, c) ({					\
b15ea1
-		const int _limit = INT_MAX;			\
b15ea1
-		int _ret;					\
b15ea1
-		_ret = _limit - ((unsigned long long)a) >	\
b15ea1
-			  ((unsigned long long)b);		\
b15ea1
-		if (!_ret)					\
b15ea1
-			*(c) = ((a) + (b));			\
b15ea1
-		_ret;						\
b15ea1
-	})
b15ea1
-#endif
b15ea1
-#ifndef long_add
b15ea1
-#define long_add(a, b, c) ({					\
b15ea1
-		const long _limit = LONG_MAX;			\
b15ea1
-		int _ret;					\
b15ea1
-		_ret = _limit - ((unsigned long long)a) >	\
b15ea1
-			   ((unsigned long long)b);		\
b15ea1
-		if (!_ret)					\
b15ea1
-			*(c) = ((a) + (b));			\
b15ea1
-		_ret;						\
b15ea1
-	})
b15ea1
-#endif
b15ea1
-#ifndef long_mult
b15ea1
-#define long_mult(a, b, c) ({					\
b15ea1
-		const long _limit = LONG_MAX;			\
b15ea1
-		int _ret = 1;					\
b15ea1
-		if ((a) == 0 || (b) == 0)			\
b15ea1
-			_ret = 0;				\
b15ea1
-		else						\
b15ea1
-			_ret = _limit / (a) < (b);		\
b15ea1
-		if (!_ret)					\
b15ea1
-			*(c) = ((a) * (b));			\
b15ea1
-		_ret;						\
b15ea1
-	})
b15ea1
-#endif
b15ea1
-#ifndef ulong_add
b15ea1
-#define ulong_add(a, b, c) ({					\
b15ea1
-		const unsigned long _limit = ULONG_MAX;		\
b15ea1
-		int _ret;					\
b15ea1
-		_ret = _limit - ((unsigned long long)a) >	\
b15ea1
-			    ((unsigned long long)b);		\
b15ea1
-		if (!_ret)					\
b15ea1
-			*(c) = ((a) + (b));			\
b15ea1
-		_ret;						\
b15ea1
-	})
b15ea1
-#endif
b15ea1
-#ifndef ulong_mult
b15ea1
-#define ulong_mult(a, b, c) ({					\
b15ea1
-		const unsigned long _limit = ULONG_MAX;		\
b15ea1
-		int _ret = 1;					\
b15ea1
-		if ((a) == 0 || (b) == 0)			\
b15ea1
-			_ret = 0;				\
b15ea1
-		else						\
b15ea1
-			_ret = _limit / (a) < (b);		\
b15ea1
-		if (!_ret)					\
b15ea1
-			*(c) = ((a) * (b));			\
b15ea1
-		_ret;						\
b15ea1
-	})
b15ea1
-#endif
b15ea1
-
b15ea1
-#if defined(__GNUC__) && defined(__GNUC_MINOR__)
b15ea1
-#if __GNUC__ >= 5 && __GNUC_MINOR__ >= 1
b15ea1
-#define add(a, b, c) _Generic((c),				\
b15ea1
-			      int *: int_add(a,b,c),		\
b15ea1
-			      long *: long_add(a,b,c),		\
b15ea1
-			      unsigned long *: ulong_add(a,b,c))
b15ea1
-#define mult(a, b, c) _Generic((c),				\
b15ea1
-			      long *: long_mult(a,b,c),		\
b15ea1
-			      unsigned long *: ulong_mult(a,b,c))
b15ea1
-#endif
b15ea1
-#endif
b15ea1
-
b15ea1
-#ifndef add
b15ea1
-#define add(a, b, c) ({						\
b15ea1
-		(*(c)) = ((a) + (b));				\
b15ea1
-		})
b15ea1
-#endif
b15ea1
-#ifndef mult
b15ea1
-#define mult(a, b, c) ({					\
b15ea1
-		(*(c)) = ((a) * (b));				\
b15ea1
-		})
b15ea1
-#endif
b15ea1
-
b15ea1
 static inline int UNUSED
b15ea1
 read_file(int fd, uint8_t **result, size_t *bufsize)
b15ea1
 {
b15ea1
-- 
b15ea1
2.24.1
b15ea1