Add standalone reallocarray.c from OpenBSD instead of rolling our own.
This commit is contained in:
1
MANIFEST
1
MANIFEST
@@ -112,6 +112,7 @@ lib/util/mktemp.c
|
||||
lib/util/parseln.c
|
||||
lib/util/progname.c
|
||||
lib/util/pw_dup.c
|
||||
lib/util/reallocarray.c
|
||||
lib/util/regress/atofoo/atofoo_test.c
|
||||
lib/util/regress/fnmatch/fnm_test.c
|
||||
lib/util/regress/fnmatch/fnm_test.in
|
||||
|
@@ -2446,6 +2446,7 @@ AC_CHECK_FUNCS([getline], [], [
|
||||
AC_CHECK_FUNCS([fgetln])
|
||||
])
|
||||
AC_CHECK_FUNCS([reallocarray], [], [
|
||||
AC_LIBOBJ(reallocarray)
|
||||
SUDO_APPEND_COMPAT_EXP(sudo_reallocarray)
|
||||
])
|
||||
dnl
|
||||
|
@@ -48,7 +48,6 @@
|
||||
#elif defined(HAVE_INTTYPES_H)
|
||||
# include <inttypes.h>
|
||||
#endif
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#define DEFAULT_TEXT_DOMAIN "sudo"
|
||||
@@ -258,17 +257,3 @@ sudo_evasprintf_v1(char **ret, const char *fmt, va_list args)
|
||||
sudo_fatal_nodebug(NULL);
|
||||
return len;
|
||||
}
|
||||
|
||||
#ifndef HAVE_REALLOCARRAY
|
||||
void *
|
||||
sudo_reallocarray(void *ptr, size_t nmemb, size_t size)
|
||||
{
|
||||
if (nmemb > SIZE_MAX / size) {
|
||||
errno = EOVERFLOW;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size *= nmemb;
|
||||
return ptr ? realloc(ptr, size) : malloc(size);
|
||||
}
|
||||
#endif /* HAVE_REALLOCARRAY */
|
||||
|
61
lib/util/reallocarray.c
Normal file
61
lib/util/reallocarray.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */
|
||||
/*
|
||||
* Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
|
||||
*
|
||||
* 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 <config.h>
|
||||
|
||||
#ifndef HAVE_REALLOCARRAY
|
||||
|
||||
#include <sys/types.h>
|
||||
#ifdef STDC_HEADERS
|
||||
# include <stdlib.h>
|
||||
# include <stddef.h>
|
||||
#else
|
||||
# ifdef HAVE_STDLIB_H
|
||||
# include <stdlib.h>
|
||||
# endif
|
||||
#endif /* STDC_HEADERS */
|
||||
#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
|
||||
# include <malloc.h>
|
||||
#endif /* HAVE_MALLOC_H && !STDC_HEADERS */
|
||||
#if defined(HAVE_STDINT_H)
|
||||
# include <stdint.h>
|
||||
#elif defined(HAVE_INTTYPES_H)
|
||||
# include <inttypes.h>
|
||||
#endif
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "sudo_compat.h"
|
||||
|
||||
/*
|
||||
* 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 *
|
||||
sudo_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);
|
||||
}
|
||||
|
||||
#endif /* HAVE_REALLOCARRAY */
|
Reference in New Issue
Block a user