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

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