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