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