Blame


1 2a00e21c 2021-09-28 thomas /* $OpenBSD$ */
2 2a00e21c 2021-09-28 thomas
3 2a00e21c 2021-09-28 thomas /*
4 2a00e21c 2021-09-28 thomas * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 2a00e21c 2021-09-28 thomas * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 2a00e21c 2021-09-28 thomas * All rights reserved
7 2a00e21c 2021-09-28 thomas * Versions of malloc and friends that check their results, and never return
8 2a00e21c 2021-09-28 thomas * failure (they call fatalx if they encounter an error).
9 2a00e21c 2021-09-28 thomas *
10 2a00e21c 2021-09-28 thomas * As far as I am concerned, the code I have written for this software
11 2a00e21c 2021-09-28 thomas * can be used freely for any purpose. Any derived versions of this
12 2a00e21c 2021-09-28 thomas * software must be clearly marked as such, and if the derived work is
13 2a00e21c 2021-09-28 thomas * incompatible with the protocol description in the RFC file, it must be
14 2a00e21c 2021-09-28 thomas * called by a name other than "ssh" or "Secure Shell".
15 2a00e21c 2021-09-28 thomas */
16 2a00e21c 2021-09-28 thomas
17 2a00e21c 2021-09-28 thomas #include <errno.h>
18 2a00e21c 2021-09-28 thomas #include <limits.h>
19 2a00e21c 2021-09-28 thomas #include <stdint.h>
20 2a00e21c 2021-09-28 thomas #include <stdio.h>
21 2a00e21c 2021-09-28 thomas #include <stdlib.h>
22 2a00e21c 2021-09-28 thomas #include <string.h>
23 2a00e21c 2021-09-28 thomas
24 2a00e21c 2021-09-28 thomas #include "got_compat.h"
25 2a00e21c 2021-09-28 thomas
26 2a00e21c 2021-09-28 thomas #include "xmalloc.h"
27 2a00e21c 2021-09-28 thomas
28 2a00e21c 2021-09-28 thomas void *
29 2a00e21c 2021-09-28 thomas xmalloc(size_t size)
30 2a00e21c 2021-09-28 thomas {
31 2a00e21c 2021-09-28 thomas void *ptr;
32 2a00e21c 2021-09-28 thomas
33 2a00e21c 2021-09-28 thomas if (size == 0) {
34 2a00e21c 2021-09-28 thomas fprintf(stderr,"xmalloc: zero size");
35 2a00e21c 2021-09-28 thomas exit (1);
36 2a00e21c 2021-09-28 thomas }
37 2a00e21c 2021-09-28 thomas ptr = malloc(size);
38 2a00e21c 2021-09-28 thomas if (ptr == NULL) {
39 2a00e21c 2021-09-28 thomas fprintf(stderr, "xmalloc: allocating %zu bytes: %s",
40 2a00e21c 2021-09-28 thomas size, strerror(errno));
41 2a00e21c 2021-09-28 thomas exit (1);
42 2a00e21c 2021-09-28 thomas }
43 2a00e21c 2021-09-28 thomas return ptr;
44 2a00e21c 2021-09-28 thomas }
45 2a00e21c 2021-09-28 thomas
46 2a00e21c 2021-09-28 thomas void *
47 2a00e21c 2021-09-28 thomas xcalloc(size_t nmemb, size_t size)
48 2a00e21c 2021-09-28 thomas {
49 2a00e21c 2021-09-28 thomas void *ptr;
50 2a00e21c 2021-09-28 thomas
51 2a00e21c 2021-09-28 thomas if (size == 0 || nmemb == 0)
52 2a00e21c 2021-09-28 thomas fprintf(stderr,"xcalloc: zero size");
53 2a00e21c 2021-09-28 thomas ptr = calloc(nmemb, size);
54 2a00e21c 2021-09-28 thomas if (ptr == NULL) {
55 2a00e21c 2021-09-28 thomas fprintf(stderr, "xcalloc: allocating %zu * %zu bytes: %s",
56 2a00e21c 2021-09-28 thomas nmemb, size, strerror(errno));
57 2a00e21c 2021-09-28 thomas exit (1);
58 2a00e21c 2021-09-28 thomas }
59 2a00e21c 2021-09-28 thomas return ptr;
60 2a00e21c 2021-09-28 thomas }
61 2a00e21c 2021-09-28 thomas
62 2a00e21c 2021-09-28 thomas void *
63 2a00e21c 2021-09-28 thomas xrealloc(void *ptr, size_t size)
64 2a00e21c 2021-09-28 thomas {
65 2a00e21c 2021-09-28 thomas return xreallocarray(ptr, 1, size);
66 2a00e21c 2021-09-28 thomas }
67 2a00e21c 2021-09-28 thomas
68 2a00e21c 2021-09-28 thomas void *
69 2a00e21c 2021-09-28 thomas xreallocarray(void *ptr, size_t nmemb, size_t size)
70 2a00e21c 2021-09-28 thomas {
71 2a00e21c 2021-09-28 thomas void *new_ptr;
72 2a00e21c 2021-09-28 thomas
73 2a00e21c 2021-09-28 thomas if (nmemb == 0 || size == 0) {
74 2a00e21c 2021-09-28 thomas fprintf(stderr, "xreallocarray: zero size");
75 2a00e21c 2021-09-28 thomas exit (1);
76 2a00e21c 2021-09-28 thomas }
77 2a00e21c 2021-09-28 thomas new_ptr = reallocarray(ptr, nmemb, size);
78 2a00e21c 2021-09-28 thomas if (new_ptr == NULL) {
79 2a00e21c 2021-09-28 thomas fprintf(stderr, "xreallocarray: allocating %zu * %zu bytes: %s",
80 2a00e21c 2021-09-28 thomas nmemb, size, strerror(errno));
81 2a00e21c 2021-09-28 thomas exit (1);
82 2a00e21c 2021-09-28 thomas }
83 2a00e21c 2021-09-28 thomas return new_ptr;
84 2a00e21c 2021-09-28 thomas }
85 2a00e21c 2021-09-28 thomas
86 2a00e21c 2021-09-28 thomas void *
87 2a00e21c 2021-09-28 thomas xrecallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size)
88 2a00e21c 2021-09-28 thomas {
89 2a00e21c 2021-09-28 thomas void *new_ptr;
90 2a00e21c 2021-09-28 thomas
91 2a00e21c 2021-09-28 thomas if (nmemb == 0 || size == 0) {
92 2a00e21c 2021-09-28 thomas fprintf(stderr,"xrecallocarray: zero size");
93 2a00e21c 2021-09-28 thomas exit (1);
94 2a00e21c 2021-09-28 thomas }
95 2a00e21c 2021-09-28 thomas new_ptr = recallocarray(ptr, oldnmemb, nmemb, size);
96 2a00e21c 2021-09-28 thomas if (new_ptr == NULL) {
97 2a00e21c 2021-09-28 thomas fprintf(stderr,"xrecallocarray: allocating %zu * %zu bytes: %s",
98 2a00e21c 2021-09-28 thomas nmemb, size, strerror(errno));
99 2a00e21c 2021-09-28 thomas exit (1);
100 2a00e21c 2021-09-28 thomas }
101 2a00e21c 2021-09-28 thomas return new_ptr;
102 2a00e21c 2021-09-28 thomas }
103 2a00e21c 2021-09-28 thomas
104 2a00e21c 2021-09-28 thomas char *
105 2a00e21c 2021-09-28 thomas xstrdup(const char *str)
106 2a00e21c 2021-09-28 thomas {
107 2a00e21c 2021-09-28 thomas char *cp;
108 2a00e21c 2021-09-28 thomas
109 2a00e21c 2021-09-28 thomas if ((cp = strdup(str)) == NULL) {
110 2a00e21c 2021-09-28 thomas fprintf(stderr,"xstrdup: %s", strerror(errno));
111 2a00e21c 2021-09-28 thomas exit (1);
112 2a00e21c 2021-09-28 thomas }
113 2a00e21c 2021-09-28 thomas return cp;
114 2a00e21c 2021-09-28 thomas }
115 2a00e21c 2021-09-28 thomas
116 2a00e21c 2021-09-28 thomas char *
117 2a00e21c 2021-09-28 thomas xstrndup(const char *str, size_t maxlen)
118 2a00e21c 2021-09-28 thomas {
119 2a00e21c 2021-09-28 thomas char *cp;
120 2a00e21c 2021-09-28 thomas
121 2a00e21c 2021-09-28 thomas if ((cp = strndup(str, maxlen)) == NULL) {
122 2a00e21c 2021-09-28 thomas fprintf(stderr,"xstrndup: %s", strerror(errno));
123 2a00e21c 2021-09-28 thomas exit (1);
124 2a00e21c 2021-09-28 thomas }
125 2a00e21c 2021-09-28 thomas return cp;
126 2a00e21c 2021-09-28 thomas }
127 2a00e21c 2021-09-28 thomas
128 2a00e21c 2021-09-28 thomas int
129 2a00e21c 2021-09-28 thomas xasprintf(char **ret, const char *fmt, ...)
130 2a00e21c 2021-09-28 thomas {
131 2a00e21c 2021-09-28 thomas va_list ap;
132 2a00e21c 2021-09-28 thomas int i;
133 2a00e21c 2021-09-28 thomas
134 2a00e21c 2021-09-28 thomas va_start(ap, fmt);
135 2a00e21c 2021-09-28 thomas i = xvasprintf(ret, fmt, ap);
136 2a00e21c 2021-09-28 thomas va_end(ap);
137 2a00e21c 2021-09-28 thomas
138 2a00e21c 2021-09-28 thomas return i;
139 2a00e21c 2021-09-28 thomas }
140 2a00e21c 2021-09-28 thomas
141 2a00e21c 2021-09-28 thomas int
142 2a00e21c 2021-09-28 thomas xvasprintf(char **ret, const char *fmt, va_list ap)
143 2a00e21c 2021-09-28 thomas {
144 2a00e21c 2021-09-28 thomas int i;
145 2a00e21c 2021-09-28 thomas
146 2a00e21c 2021-09-28 thomas i = vasprintf(ret, fmt, ap);
147 2a00e21c 2021-09-28 thomas
148 2a00e21c 2021-09-28 thomas if (i == -1) {
149 2a00e21c 2021-09-28 thomas fprintf(stderr,"xasprintf: %s", strerror(errno));
150 2a00e21c 2021-09-28 thomas exit (1);
151 2a00e21c 2021-09-28 thomas }
152 2a00e21c 2021-09-28 thomas
153 2a00e21c 2021-09-28 thomas return i;
154 2a00e21c 2021-09-28 thomas }
155 2a00e21c 2021-09-28 thomas
156 2a00e21c 2021-09-28 thomas int
157 2a00e21c 2021-09-28 thomas xsnprintf(char *str, size_t len, const char *fmt, ...)
158 2a00e21c 2021-09-28 thomas {
159 2a00e21c 2021-09-28 thomas va_list ap;
160 2a00e21c 2021-09-28 thomas int i;
161 2a00e21c 2021-09-28 thomas
162 2a00e21c 2021-09-28 thomas va_start(ap, fmt);
163 2a00e21c 2021-09-28 thomas i = xvsnprintf(str, len, fmt, ap);
164 2a00e21c 2021-09-28 thomas va_end(ap);
165 2a00e21c 2021-09-28 thomas
166 2a00e21c 2021-09-28 thomas return i;
167 2a00e21c 2021-09-28 thomas }
168 2a00e21c 2021-09-28 thomas
169 2a00e21c 2021-09-28 thomas int
170 2a00e21c 2021-09-28 thomas xvsnprintf(char *str, size_t len, const char *fmt, va_list ap)
171 2a00e21c 2021-09-28 thomas {
172 2a00e21c 2021-09-28 thomas int i;
173 2a00e21c 2021-09-28 thomas
174 2a00e21c 2021-09-28 thomas if (len > INT_MAX)
175 2a00e21c 2021-09-28 thomas fprintf(stderr,"xsnprintf: len > INT_MAX");
176 2a00e21c 2021-09-28 thomas
177 2a00e21c 2021-09-28 thomas i = vsnprintf(str, len, fmt, ap);
178 2a00e21c 2021-09-28 thomas
179 2a00e21c 2021-09-28 thomas if (i < 0 || i >= (int)len) {
180 2a00e21c 2021-09-28 thomas fprintf(stderr,"xsnprintf: overflow");
181 2a00e21c 2021-09-28 thomas exit (1);
182 2a00e21c 2021-09-28 thomas }
183 2a00e21c 2021-09-28 thomas
184 2a00e21c 2021-09-28 thomas return i;
185 2a00e21c 2021-09-28 thomas }