From 5fad442525c47c6f3e75d81b6368ec1fca5eccd1 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 10 Mar 2024 18:39:41 +0100 Subject: Add the bufalloc_noterm() function --- lib/alloc/bufalloc.c | 23 +---------------------- lib/alloc/bufalloc_noterm.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 lib/alloc/bufalloc_noterm.c (limited to 'lib') diff --git a/lib/alloc/bufalloc.c b/lib/alloc/bufalloc.c index dcb0a93..c688ab1 100644 --- a/lib/alloc/bufalloc.c +++ b/lib/alloc/bufalloc.c @@ -1,31 +1,10 @@ -#include -#if __has_include() -# include -# ifdef __APPLE__ -# warning "stdckdint.h now available on Mac; remove manual ckd_*() code" -# endif -#elifdef __GNUC__ -# define ckd_add(r, a, b) ((bool)__builtin_add_overflow(a, b, r)) -# define ckd_mul(r, a, b) ((bool)__builtin_mul_overflow(a, b, r)) -#else -# define ckd_add(r, a, b) (*(r) = (a) + (b)) -# define ckd_mul(r, a, b) (*(r) = (a) * (b)) -# warning "ckd_*() not supported on the current platform" -#endif -#include - #include "alloc.h" #include "errors.h" void * bufalloc(void *p, size_t n, size_t m) { - if (ckd_mul(&n, n, m)) { - errno = EOVERFLOW; - err(__func__); - } - - if (!(p = realloc(p, n))) + if ((p = bufalloc_noterm(p, n, m)) == nullptr) err(__func__); return p; } diff --git a/lib/alloc/bufalloc_noterm.c b/lib/alloc/bufalloc_noterm.c new file mode 100644 index 0000000..8258521 --- /dev/null +++ b/lib/alloc/bufalloc_noterm.c @@ -0,0 +1,28 @@ +#include +#if __has_include() +# include +# ifdef __APPLE__ +# warning "stdckdint.h now available on Mac; remove manual ckd_*() code" +# endif +#elifdef __GNUC__ +# define ckd_add(r, a, b) ((bool)__builtin_add_overflow(a, b, r)) +# define ckd_mul(r, a, b) ((bool)__builtin_mul_overflow(a, b, r)) +#else +# define ckd_add(r, a, b) (*(r) = (a) + (b)) +# define ckd_mul(r, a, b) (*(r) = (a) * (b)) +# warning "ckd_*() not supported on the current platform" +#endif +#include + +#include "alloc.h" + +void * +bufalloc_noterm(void *p, size_t n, size_t m) +{ + if (ckd_mul(&n, n, m)) { + errno = EOVERFLOW; + return nullptr; + } + + return realloc(p, n); +} -- cgit v1.2.3