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