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