xref: /netbsd-src/external/bsd/ntp/dist/libntp/emalloc.c (revision 5d681e99340ceeda0a51163a65763ffea6d9a189)
1 /*	$NetBSD: emalloc.c,v 1.5 2015/07/10 14:20:32 christos Exp $	*/
2 
3 /*
4  * emalloc - return new memory obtained from the system.  Belch if none.
5  */
6 #include <config.h>
7 #include "ntp_types.h"
8 #include "ntp_malloc.h"
9 #include "ntp_syslog.h"
10 #include "ntp_stdlib.h"
11 
12 
13 /*
14  * When using the debug MS CRT allocator, each allocation stores the
15  * callsite __FILE__ and __LINE__, which is then displayed at process
16  * termination, to track down leaks.  We don't want all of our
17  * allocations to show up as coming from emalloc.c, so we preserve the
18  * original callsite's source file and line using macros which pass
19  * __FILE__ and __LINE__ as parameters to these routines.
20  * Other debug malloc implementations can be used by defining
21  * EREALLOC_IMPL() as ports/winnt/include/config.h does.
22  */
23 
24 void *
25 ereallocz(
26 	void *	ptr,
27 	size_t	newsz,
28 	size_t	priorsz,
29 	int	zero_init
30 #ifdef EREALLOC_CALLSITE		/* ntp_malloc.h */
31 			 ,
32 	const char *	file,
33 	int		line
34 #endif
35 	)
36 {
37 	char *	mem;
38 	size_t	allocsz;
39 
40 	if (0 == newsz)
41 		allocsz = 1;
42 	else
43 		allocsz = newsz;
44 
45 	mem = EREALLOC_IMPL(ptr, allocsz, file, line);
46 	if (NULL == mem) {
47 		msyslog_term = TRUE;
48 #ifndef EREALLOC_CALLSITE
49 		msyslog(LOG_ERR, "fatal out of memory (%lu bytes)",
50 			(u_long)newsz);
51 #else
52 		msyslog(LOG_ERR,
53 			"fatal out of memory %s line %d (%lu bytes)",
54 			file, line, (u_long)newsz);
55 #endif
56 		exit(1);
57 	}
58 
59 	if (zero_init && newsz > priorsz)
60 		zero_mem(mem + priorsz, newsz - priorsz);
61 
62 	return mem;
63 }
64 
65 /* oreallocarray.c is licensed under the following:
66  * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
67  *
68  * Permission to use, copy, modify, and distribute this software for any
69  * purpose with or without fee is hereby granted, provided that the above
70  * copyright notice and this permission notice appear in all copies.
71  *
72  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
73  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
74  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
75  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
76  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
77  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
78  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
79  */
80 
81 #include <stdint.h>
82 
83 /*
84  * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
85  * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
86  */
87 #define MUL_NO_OVERFLOW	((size_t)1 << (sizeof(size_t) * 4))
88 
89 void *
90 oreallocarray(
91 	void *optr,
92 	size_t nmemb,
93 	size_t size
94 #ifdef EREALLOC_CALLSITE		/* ntp_malloc.h */
95 	,
96 	const char *	file,
97 	int		line
98 #endif
99 	)
100 {
101 	if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
102 	    nmemb > 0 && SIZE_MAX / nmemb < size) {
103 #ifndef EREALLOC_CALLSITE
104 		msyslog(LOG_ERR, "fatal allocation size overflow");
105 #else
106 		msyslog(LOG_ERR,
107 			"fatal allocation size overflow %s line %d",
108 			file, line);
109 #endif
110 		exit(1);
111 	}
112 #ifndef EREALLOC_CALLSITE
113 	return ereallocz(optr, (size * nmemb), 0, FALSE);
114 #else
115 	return ereallocz(optr, (size * nmemb), 0, FALSE, file, line);
116 #endif
117 }
118 
119 char *
120 estrdup_impl(
121 	const char *	str
122 #ifdef EREALLOC_CALLSITE
123 			   ,
124 	const char *	file,
125 	int		line
126 #endif
127 	)
128 {
129 	char *	copy;
130 	size_t	bytes;
131 
132 	bytes = strlen(str) + 1;
133 	copy = ereallocz(NULL, bytes, 0, FALSE
134 #ifdef EREALLOC_CALLSITE
135 			 , file, line
136 #endif
137 			 );
138 	memcpy(copy, str, bytes);
139 
140 	return copy;
141 }
142 
143 
144 #if 0
145 #ifndef EREALLOC_CALLSITE
146 void *
147 emalloc(size_t newsz)
148 {
149 	return ereallocz(NULL, newsz, 0, FALSE);
150 }
151 #endif
152 #endif
153 
154