Blame


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