]> git.cameronkatri.com Git - mandoc.git/blob - compat_reallocarray.c
Improve build system and autodetection.
[mandoc.git] / compat_reallocarray.c
1 #include "config.h"
2
3 #if HAVE_REALLOCARRAY
4
5 int dummy;
6
7 #else
8
9 /* $OpenBSD: malloc.c,v 1.158 2014/04/23 15:07:27 tedu Exp $ */
10 /*
11 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
12 *
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 */
25 #include <sys/types.h>
26 #include <errno.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29
30 #define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4))
31
32 void *
33 reallocarray(void *optr, size_t nmemb, size_t size)
34 {
35 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
36 nmemb > 0 && SIZE_MAX / nmemb < size) {
37 errno = ENOMEM;
38 return NULL;
39 }
40 return realloc(optr, size * nmemb);
41 }
42
43 #endif /*!HAVE_REALLOCARRAY*/