commit abe0273c040fe052cdead5144db761fbd52ce7b7 from: Omar Polo date: Tue Feb 06 14:16:49 2024 UTC add compat for reallocarray() commit - d163c2104eb8f8603c4dfc4c9ad566511eac143b commit + abe0273c040fe052cdead5144db761fbd52ce7b7 blob - /dev/null blob + a12eb220f52fd230cbbec83e2892133a6dcbeaeb (mode 644) --- /dev/null +++ compat/reallocarray.c @@ -0,0 +1,40 @@ +/* $OpenBSD: reallocarray.c,v 1.3 2015/09/13 08:31:47 guenther 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. + */ + +#include "compat.h" + +#include +#include +#include +#include + +/* + * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX + * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW + */ +#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); +} blob - 2d2216f7e204c10465ec1431d32f3643fe82713d blob + de06e6b654820fabe2d2634479ec69a0d184441c --- compat.h +++ compat.h @@ -125,6 +125,10 @@ const char *getprogname(void); void *memmem(const void*, size_t, const void*, size_t); #endif +#ifndef HAVE_REALLOCARRAY +void *reallocarray(void*, size_t, size_t); +#endif + #ifndef HAVE_RECALLOCARRAY void *recallocarray(void*, size_t, size_t, size_t); #endif blob - a8ee2a6f78f72f3a016b5eec18422499923c42bb blob + 98503dd91a0416d6137f853e09b368fd9f171227 --- configure.ac +++ configure.ac @@ -43,6 +43,7 @@ AC_REPLACE_FUNCS([ getdtablesize \ getprogname \ memmem \ + reallocarray \ recallocarray \ strcasestr \ strlcat \