1 3d89457c 2024-06-18 thomas.ad /* $OpenBSD$ */
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).
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".
17 3d89457c 2024-06-18 thomas.ad #include "compat.h"
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>
26 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
29 3d89457c 2024-06-18 thomas.ad xmalloc(size_t size)
31 3d89457c 2024-06-18 thomas.ad void *ptr;
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;
43 3d89457c 2024-06-18 thomas.ad xcalloc(size_t nmemb, size_t size)
45 3d89457c 2024-06-18 thomas.ad void *ptr;
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;
57 3d89457c 2024-06-18 thomas.ad xrealloc(void *ptr, size_t size)
59 3d89457c 2024-06-18 thomas.ad return xreallocarray(ptr, 1, size);
63 3d89457c 2024-06-18 thomas.ad xreallocarray(void *ptr, size_t nmemb, size_t size)
65 3d89457c 2024-06-18 thomas.ad void *new_ptr;
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;
77 3d89457c 2024-06-18 thomas.ad xrecallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size)
79 3d89457c 2024-06-18 thomas.ad void *new_ptr;
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;
91 3d89457c 2024-06-18 thomas.ad xstrdup(const char *str)
93 3d89457c 2024-06-18 thomas.ad char *cp;
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;
100 3d89457c 2024-06-18 thomas.ad char *
101 3d89457c 2024-06-18 thomas.ad xstrndup(const char *str, size_t maxlen)
103 3d89457c 2024-06-18 thomas.ad char *cp;
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;
111 3d89457c 2024-06-18 thomas.ad xasprintf(char **ret, const char *fmt, ...)
113 3d89457c 2024-06-18 thomas.ad va_list ap;
114 3d89457c 2024-06-18 thomas.ad int i;
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);
120 3d89457c 2024-06-18 thomas.ad return i;
124 3d89457c 2024-06-18 thomas.ad xvasprintf(char **ret, const char *fmt, va_list ap)
126 3d89457c 2024-06-18 thomas.ad int i;
128 3d89457c 2024-06-18 thomas.ad i = vasprintf(ret, fmt, ap);
130 3d89457c 2024-06-18 thomas.ad if (i == -1)
131 3d89457c 2024-06-18 thomas.ad errx(1, "xasprintf: %s", strerror(errno));
133 3d89457c 2024-06-18 thomas.ad return i;
137 3d89457c 2024-06-18 thomas.ad xsnprintf(char *str, size_t len, const char *fmt, ...)
139 3d89457c 2024-06-18 thomas.ad va_list ap;
140 3d89457c 2024-06-18 thomas.ad int i;
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);
146 3d89457c 2024-06-18 thomas.ad return i;
150 3d89457c 2024-06-18 thomas.ad xvsnprintf(char *str, size_t len, const char *fmt, va_list ap)
152 3d89457c 2024-06-18 thomas.ad int i;
154 3d89457c 2024-06-18 thomas.ad if (len > INT_MAX)
155 3d89457c 2024-06-18 thomas.ad errx(1, "xsnprintf: len > INT_MAX");
157 3d89457c 2024-06-18 thomas.ad i = vsnprintf(str, len, fmt, ap);
159 3d89457c 2024-06-18 thomas.ad if (i < 0 || i >= (int)len)
160 3d89457c 2024-06-18 thomas.ad errx(1, "xsnprintf: overflow");
162 3d89457c 2024-06-18 thomas.ad return i;