Blame


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