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