We don't need separate macros for size_t and uint64_t since the compiler
builtins are generic. If we need to port to a complier that does not have
generic builtins, it is likely that we can do this in a generic way.
Signed-off-by: Nir Soffer <nsoffer(a)redhat.com>
---
common/include/checked-overflow.h | 18 ++++--------------
common/utils/vector.c | 10 +++++-----
2 files changed, 9 insertions(+), 19 deletions(-)
diff --git a/common/include/checked-overflow.h b/common/include/checked-overflow.h
index a84b82d..ddc4b48 100644
--- a/common/include/checked-overflow.h
+++ b/common/include/checked-overflow.h
@@ -46,24 +46,14 @@
#error "this file may need to be ported to your compiler"
#endif
-/* Add two uint64_t values. *r = a + b
+/* Add two values. *r = a + b
* Returns true if overflow happened.
*/
-#define ADD_UINT64_T_OVERFLOW(a, b, r) __builtin_add_overflow((a), (b), (r))
+#define ADD_OVERFLOW(a, b, r) __builtin_add_overflow((a), (b), (r))
-/* Multiply two uint64_t values. *r = a * b
+/* Multiply two values. *r = a * b
* Returns true if overflow happened.
*/
-#define MUL_UINT64_T_OVERFLOW(a, b, r) __builtin_mul_overflow((a), (b), (r))
-
-/* Add two size_t values. *r = a + b
- * Returns true if overflow happened.
- */
-#define ADD_SIZE_T_OVERFLOW(a, b, r) __builtin_add_overflow((a), (b), (r))
-
-/* Multiply two size_t values. *r = a * b
- * Returns true if overflow happened.
- */
-#define MUL_SIZE_T_OVERFLOW(a, b, r) __builtin_mul_overflow((a), (b), (r))
+#define MUL_OVERFLOW(a, b, r) __builtin_mul_overflow((a), (b), (r))
#endif /* NBDKIT_CHECKED_OVERFLOW_H */
diff --git a/common/utils/vector.c b/common/utils/vector.c
index 550e624..ee75915 100644
--- a/common/utils/vector.c
+++ b/common/utils/vector.c
@@ -49,8 +49,8 @@ generic_vector_reserve (struct generic_vector *v, size_t n, size_t
itemsize)
* reqcap = v->cap + n
* reqbytes = reqcap * itemsize
*/
- if (ADD_SIZE_T_OVERFLOW (v->cap, n, &reqcap) ||
- MUL_SIZE_T_OVERFLOW (reqcap, itemsize, &reqbytes)) {
+ if (ADD_OVERFLOW (v->cap, n, &reqcap) ||
+ MUL_OVERFLOW (reqcap, itemsize, &reqbytes)) {
errno = ENOMEM;
return -1;
}
@@ -60,9 +60,9 @@ generic_vector_reserve (struct generic_vector *v, size_t n, size_t
itemsize)
* newcap = v->cap + (v->cap + 1) / 2
* newbytes = newcap * itemsize
*/
- if (ADD_SIZE_T_OVERFLOW (v->cap, 1, &t) ||
- ADD_SIZE_T_OVERFLOW (v->cap, t/2, &newcap) ||
- MUL_SIZE_T_OVERFLOW (newcap, itemsize, &newbytes) ||
+ if (ADD_OVERFLOW (v->cap, 1, &t) ||
+ ADD_OVERFLOW (v->cap, t/2, &newcap) ||
+ MUL_OVERFLOW (newcap, itemsize, &newbytes) ||
newbytes < reqbytes) {
/* If that either overflows or is less than the minimum requested,
* fall back to the requested capacity.
--
2.31.1