From ab69ae8374ec556f80ff1a5559f10da60996f920 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 9 May 2016 00:33:47 +0200 Subject: [PATCH] Add OpenBSD reallocarray(3) v1.2 Signed-off-by: Joachim Nilsson --- Makefile.am | 5 +++-- README.md | 4 ++++ lite.h | 2 ++ reallocarray.c | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 reallocarray.c diff --git a/Makefile.am b/Makefile.am index 73adbef..0ea9e26 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,8 +4,9 @@ lib_LTLIBRARIES = libite.la pkgincludedir = $(includedir)/lite pkginclude_HEADERS = lite.h queue.h conio.h tree.h libite_la_SOURCES = chomp.c copyfile.c dir.c fexist.c fisdir.c fmode.c fsendfile.c \ - ifconfig.c lfile.c makepath.c pidfile.c pidfilefn.c progress.c \ - rsync.c strlcpy.c strlcat.c strtonum.c tempfile.c tree.c + ifconfig.c lfile.c makepath.c pidfile.c pidfilefn.c progress.c \ + reallocarray.c rsync.c strlcpy.c strlcat.c strtonum.c tempfile.c \ + tree.c libite_la_CPPFLAGS = -D_GNU_SOURCE libite_la_CFLAGS = -W -Wall -Wextra diff --git a/README.md b/README.md index b9f6c1b..4e08dea 100644 --- a/README.md +++ b/README.md @@ -296,6 +296,10 @@ The following are popular OpenBSD functions and highly useful macros. file `__pidfile_name`, similar to `__progname`. See previous section for details. +- `reallocarray(ptr, nmemb, size)` + + http://man.openbsd.org/OpenBSD-current/man3/reallocarray.3 + - `strlcpy(dst, src, len)` http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/strlcpy.3 diff --git a/lite.h b/lite.h index 050facb..74786b1 100644 --- a/lite.h +++ b/lite.h @@ -68,6 +68,8 @@ int pidfile_signal(const char *pidfile, int signal); pid_t pidfile_read (const char *pidfile); pid_t pidfile_poll (const char *pidfile); +void *reallocarray(void *optr, size_t nmemb, size_t size); + #ifndef strlcpy size_t strlcpy (char *dst, const char *src, size_t siz); #endif diff --git a/reallocarray.c b/reallocarray.c new file mode 100644 index 0000000..68baca2 --- /dev/null +++ b/reallocarray.c @@ -0,0 +1,38 @@ +/* $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 * +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); +}