[PATCH libnbd] common/include/checked-overflow.h: Simplify
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@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
On Wed, Nov 10, 2021 at 08:49:27PM +0200, Nir Soffer wrote:
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@redhat.com> --- common/include/checked-overflow.h | 18 ++++-------------- common/utils/vector.c | 10 +++++----- 2 files changed, 9 insertions(+), 19 deletions(-)
ACK. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On Wed, Nov 10, 2021 at 12:56:26PM -0600, Eric Blake wrote:
On Wed, Nov 10, 2021 at 08:49:27PM +0200, Nir Soffer wrote:
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@redhat.com> --- common/include/checked-overflow.h | 18 ++++-------------- common/utils/vector.c | 10 +++++----- 2 files changed, 9 insertions(+), 19 deletions(-)
ACK.
I'll go with what Eric says, particularly because there's an example of this in gnulib (under an incompatible license, so we cannot use it): https://github.com/coreutils/gnulib/blob/50d64b72f2487efcaca6a8b240bc80168e5... Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
On Wed, Nov 10, 2021 at 10:07 PM Richard W.M. Jones <rjones@redhat.com> wrote:
On Wed, Nov 10, 2021 at 12:56:26PM -0600, Eric Blake wrote:
On Wed, Nov 10, 2021 at 08:49:27PM +0200, Nir Soffer wrote:
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@redhat.com> --- common/include/checked-overflow.h | 18 ++++-------------- common/utils/vector.c | 10 +++++----- 2 files changed, 9 insertions(+), 19 deletions(-)
ACK.
I'll go with what Eric says, particularly because there's an example of this in gnulib (under an incompatible license, so we cannot use it):
https://github.com/coreutils/gnulib/blob/50d64b72f2487efcaca6a8b240bc80168e5...
Pushed to libnbd as 46fa6ecc7422e830f10d64b62c808abe6f8b49af. I'll push same change to nbdkit.
On Thu, Nov 11, 2021 at 9:15 PM Nir Soffer <nsoffer@redhat.com> wrote:
On Wed, Nov 10, 2021 at 10:07 PM Richard W.M. Jones <rjones@redhat.com> wrote:
On Wed, Nov 10, 2021 at 12:56:26PM -0600, Eric Blake wrote:
On Wed, Nov 10, 2021 at 08:49:27PM +0200, Nir Soffer wrote:
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@redhat.com> --- common/include/checked-overflow.h | 18 ++++-------------- common/utils/vector.c | 10 +++++----- 2 files changed, 9 insertions(+), 19 deletions(-)
ACK.
I'll go with what Eric says, particularly because there's an example of this in gnulib (under an incompatible license, so we cannot use it):
https://github.com/coreutils/gnulib/blob/50d64b72f2487efcaca6a8b240bc80168e5...
Pushed to libnbd as 46fa6ecc7422e830f10d64b62c808abe6f8b49af.
I'll push same change to nbdkit.
Pushed to nbdkit as b31859402d1404ba0433ba2eb64f2047ae21677b.
participants (3)
-
Eric Blake -
Nir Soffer -
Richard W.M. Jones