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