Blob


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