From c071586ae53965cbdc8c10643b18e68dc9d2d56b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ni=C3=B1o=20D=C3=ADaz?= Date: Sat, 27 Jan 2018 00:30:13 +0000 Subject: [PATCH] Remove dependency of reallocarray() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By removing this dependency, all of the code of this repository is licensed under the MIT license. Signed-off-by: Antonio Niño Díaz --- LICENSE.rst | 9 +-------- Makefile | 2 -- include/extern/reallocarray.h | 23 ---------------------- src/asm/main.c | 29 ++++++++++++++++++--------- src/extern/reallocarray.c | 37 ----------------------------------- 5 files changed, 21 insertions(+), 79 deletions(-) delete mode 100644 include/extern/reallocarray.h delete mode 100644 src/extern/reallocarray.c diff --git a/LICENSE.rst b/LICENSE.rst index e4e21854..ec72fb37 100644 --- a/LICENSE.rst +++ b/LICENSE.rst @@ -1,5 +1,4 @@ -LICENSE -======= +The MIT License Copyright (c) 1997-2018, Carsten Sorensen and RGBDS contributors. @@ -20,9 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -Other ------ - -extern/reallocarray.c is derived from the OpenBSD Project, -http://www.openbsd.org, and is released under the ISC license. diff --git a/Makefile b/Makefile index 45ec6c36..611b1ec8 100644 --- a/Makefile +++ b/Makefile @@ -57,11 +57,9 @@ rgbasm_obj := \ src/asm/rpn.o \ src/asm/symbol.o \ src/extern/err.o \ - src/extern/reallocarray.o \ src/extern/utf8decoder.o \ src/extern/version.o - src/asm/asmy.h: src/asm/asmy.c src/asm/locallex.o src/asm/globlex.o src/asm/lexer.o: src/asm/asmy.h diff --git a/include/extern/reallocarray.h b/include/extern/reallocarray.h deleted file mode 100644 index 6517fe5b..00000000 --- a/include/extern/reallocarray.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * This file is part of RGBDS. - * - * Copyright (c) 2015-2018, RGBDS contributors. - * - * SPDX-License-Identifier: MIT - */ - -#ifndef EXTERN_REALLOCARRAY_H -#define EXTERN_REALLOCARRAY_H - -#ifdef REALLOCARRAY_IN_LIBC - -#include - -#else /* REALLOCARRAY_IN_LIBC */ - -#define reallocarray rgbds_reallocarray -void *reallocarray(void *, size_t, size_t); - -#endif /* REALLOCARRAY_IN_LIBC */ - -#endif /* EXTERN_REALLOCARRAY_H */ diff --git a/src/asm/main.c b/src/asm/main.c index 60b64367..1c2a18cd 100644 --- a/src/asm/main.c +++ b/src/asm/main.c @@ -22,13 +22,14 @@ #include "asm/main.h" #include "extern/err.h" -#include "extern/reallocarray.h" #include "extern/version.h" extern int yyparse(void); -int32_t cldefines_index; -int32_t cldefines_size; +size_t cldefines_index; +size_t cldefines_numindices; +size_t cldefines_bufsize; +const size_t cldefine_entrysize = 2 * sizeof(void *); char **cldefines; clock_t nStartClock, nEndClock; @@ -192,10 +193,18 @@ void opt_AddDefine(char *s) { char *value, *equals; - if (cldefines_index >= cldefines_size) { - cldefines_size *= 2; - cldefines = reallocarray(cldefines, cldefines_size, - 2 * sizeof(void *)); + if (cldefines_index >= cldefines_numindices) { + /* Check for overflows */ + if ((cldefines_numindices * 2) < cldefines_numindices) + fatalerror("No memory for command line defines"); + + if ((cldefines_bufsize * 2) < cldefines_bufsize) + fatalerror("No memory for command line defines"); + + cldefines_numindices *= 2; + cldefines_bufsize *= 2; + + cldefines = realloc(cldefines, cldefines_bufsize); if (!cldefines) fatalerror("No memory for command line defines"); } @@ -288,8 +297,10 @@ int main(int argc, char *argv[]) dependfile = NULL; - cldefines_size = 32; - cldefines = reallocarray(cldefines, cldefines_size, 2 * sizeof(void *)); + /* Initial number of allocated elements in array */ + cldefines_numindices = 32; + cldefines_bufsize = cldefines_numindices * cldefine_entrysize; + cldefines = malloc(cldefines_bufsize); if (!cldefines) fatalerror("No memory for command line defines"); diff --git a/src/extern/reallocarray.c b/src/extern/reallocarray.c deleted file mode 100644 index c2183b56..00000000 --- a/src/extern/reallocarray.c +++ /dev/null @@ -1,37 +0,0 @@ -/* $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. - */ - -#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 *rgbds_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); -}