From 6eb1487311a5f12857566dd226eab9bd6354b478 Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Mon, 6 May 2024 11:03:22 -0700 Subject: [PATCH] implement reallocarray on macOS --- .idea/misc.xml | 3 +++ include/lfmacos.h | 1 + src/macos.c | 37 +++++++++++++++++++++++++++++++++++++ src/vector.c | 8 ++++---- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 79b3c94..0b76fe5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ + + \ No newline at end of file diff --git a/include/lfmacos.h b/include/lfmacos.h index fe54997..19f89cf 100644 --- a/include/lfmacos.h +++ b/include/lfmacos.h @@ -18,6 +18,7 @@ typedef struct { ProcessData *new_ProcessData(); int update_process(pid_t pid, ProcessData *proc); +void *reallocarray(void *optr, size_t nmemb, size_t size); #endif /* defined(__APPLE__) || defined(__MACH__) */ #endif /* LIBFLINT_MACOS_H */ diff --git a/src/macos.c b/src/macos.c index 57cd48e..3bbcf1c 100644 --- a/src/macos.c +++ b/src/macos.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "lfmacos.h" @@ -43,3 +44,39 @@ int update_process(pid_t pid, ProcessData *proc) { return 0; } + +/* reallocarray is reimplemented here for macOS because Apple doesn't expose + * their implementation. This is taken straight from the OpenBSD source as + * shown in the below copyright notice + */ + +/* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */ +/* + * Copyright (c) 2008 Otto Moerbeek + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* OPENBSD ORIGINAL: lib/libc/stdlib/reallocarray.c */ + +#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) + +void *reallocarray(void *optr, size_t nmemb, size_t size) +{ + if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && + nmemb > 0 && SIZE_MAX / nmemb < size) { + errno = ENOMEM; + return NULL; + } + return realloc(optr, size * nmemb); +} diff --git a/src/vector.c b/src/vector.c index 2179fc5..bab701f 100644 --- a/src/vector.c +++ b/src/vector.c @@ -5,6 +5,10 @@ #include #endif +#if defined(__APPLE__) || defined(__MACH__) +#include "lfmacos.h" +#endif + #include "lfvector.h" #define VEC_INIT_CAP 2 @@ -32,11 +36,7 @@ int vec_init_with_capacity(Vector *vec, void (*destroy)(void *data), size_t capa static int vec_grow(Vector *const vec) { vec->capacity *= 2; -#ifdef __OpenBSD__ vec->elements = reallocarray(vec->elements, vec->capacity, sizeof(void *)); -#else - vec->elements = reallocf(vec->elements, sizeof(void *) * vec->capacity); -#endif if (vec->elements == NULL) { return -1;