xref: /minix3/lib/libc/stdlib/reallocarr.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: reallocarr.c,v 1.5 2015/08/20 22:27:49 kamil Exp $ */
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2015 Joerg Sonnenberger <joerg@NetBSD.org>.
5*0a6a1f1dSLionel Sambuc  * All rights reserved.
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc  * are met:
10*0a6a1f1dSLionel Sambuc  *
11*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
12*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
13*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
14*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
15*0a6a1f1dSLionel Sambuc  *    the documentation and/or other materials provided with the
16*0a6a1f1dSLionel Sambuc  *    distribution.
17*0a6a1f1dSLionel Sambuc  *
18*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*0a6a1f1dSLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*0a6a1f1dSLionel Sambuc  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21*0a6a1f1dSLionel Sambuc  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
22*0a6a1f1dSLionel Sambuc  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23*0a6a1f1dSLionel Sambuc  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24*0a6a1f1dSLionel Sambuc  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*0a6a1f1dSLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26*0a6a1f1dSLionel Sambuc  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27*0a6a1f1dSLionel Sambuc  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28*0a6a1f1dSLionel Sambuc  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*0a6a1f1dSLionel Sambuc  * SUCH DAMAGE.
30*0a6a1f1dSLionel Sambuc  */
31*0a6a1f1dSLionel Sambuc 
32*0a6a1f1dSLionel Sambuc #if HAVE_NBTOOL_CONFIG_H
33*0a6a1f1dSLionel Sambuc #include "nbtool_config.h"
34*0a6a1f1dSLionel Sambuc #endif
35*0a6a1f1dSLionel Sambuc 
36*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
37*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: reallocarr.c,v 1.5 2015/08/20 22:27:49 kamil Exp $");
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc #include "namespace.h"
40*0a6a1f1dSLionel Sambuc #include <errno.h>
41*0a6a1f1dSLionel Sambuc /* Old POSIX has SIZE_MAX in limits.h */
42*0a6a1f1dSLionel Sambuc #include <limits.h>
43*0a6a1f1dSLionel Sambuc #include <stdint.h>
44*0a6a1f1dSLionel Sambuc #include <stdlib.h>
45*0a6a1f1dSLionel Sambuc #include <string.h>
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc #ifdef _LIBC
48*0a6a1f1dSLionel Sambuc #ifdef __weak_alias
__weak_alias(reallocarr,_reallocarr)49*0a6a1f1dSLionel Sambuc __weak_alias(reallocarr, _reallocarr)
50*0a6a1f1dSLionel Sambuc #endif
51*0a6a1f1dSLionel Sambuc #endif
52*0a6a1f1dSLionel Sambuc 
53*0a6a1f1dSLionel Sambuc #define SQRT_SIZE_MAX (((size_t)1) << (sizeof(size_t) * CHAR_BIT / 2))
54*0a6a1f1dSLionel Sambuc 
55*0a6a1f1dSLionel Sambuc #if !HAVE_REALLOCARR
56*0a6a1f1dSLionel Sambuc int
57*0a6a1f1dSLionel Sambuc reallocarr(void *ptr, size_t number, size_t size)
58*0a6a1f1dSLionel Sambuc {
59*0a6a1f1dSLionel Sambuc 	int saved_errno, result;
60*0a6a1f1dSLionel Sambuc 	void *optr;
61*0a6a1f1dSLionel Sambuc 	void *nptr;
62*0a6a1f1dSLionel Sambuc 
63*0a6a1f1dSLionel Sambuc 	saved_errno = errno;
64*0a6a1f1dSLionel Sambuc 	memcpy(&optr, ptr, sizeof(ptr));
65*0a6a1f1dSLionel Sambuc 	if (number == 0 || size == 0) {
66*0a6a1f1dSLionel Sambuc 		free(optr);
67*0a6a1f1dSLionel Sambuc 		nptr = NULL;
68*0a6a1f1dSLionel Sambuc 		memcpy(ptr, &nptr, sizeof(ptr));
69*0a6a1f1dSLionel Sambuc 		errno = saved_errno;
70*0a6a1f1dSLionel Sambuc 		return 0;
71*0a6a1f1dSLionel Sambuc 	}
72*0a6a1f1dSLionel Sambuc 
73*0a6a1f1dSLionel Sambuc 	/*
74*0a6a1f1dSLionel Sambuc 	 * Try to avoid division here.
75*0a6a1f1dSLionel Sambuc 	 *
76*0a6a1f1dSLionel Sambuc 	 * It isn't possible to overflow during multiplication if neither
77*0a6a1f1dSLionel Sambuc 	 * operand uses any of the most significant half of the bits.
78*0a6a1f1dSLionel Sambuc 	 */
79*0a6a1f1dSLionel Sambuc 	if (__predict_false((number|size) >= SQRT_SIZE_MAX &&
80*0a6a1f1dSLionel Sambuc 	                    number > SIZE_MAX / size)) {
81*0a6a1f1dSLionel Sambuc 		errno = saved_errno;
82*0a6a1f1dSLionel Sambuc 		return EOVERFLOW;
83*0a6a1f1dSLionel Sambuc 	}
84*0a6a1f1dSLionel Sambuc 
85*0a6a1f1dSLionel Sambuc 	nptr = realloc(optr, number * size);
86*0a6a1f1dSLionel Sambuc 	if (__predict_false(nptr == NULL)) {
87*0a6a1f1dSLionel Sambuc 		result = errno;
88*0a6a1f1dSLionel Sambuc 	} else {
89*0a6a1f1dSLionel Sambuc 		result = 0;
90*0a6a1f1dSLionel Sambuc 		memcpy(ptr, &nptr, sizeof(ptr));
91*0a6a1f1dSLionel Sambuc 	}
92*0a6a1f1dSLionel Sambuc 	errno = saved_errno;
93*0a6a1f1dSLionel Sambuc 	return result;
94*0a6a1f1dSLionel Sambuc }
95*0a6a1f1dSLionel Sambuc #endif
96