1 2a00e21c 2021-09-28 thomas /* $OpenBSD$ */
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).
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".
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>
24 2a00e21c 2021-09-28 thomas #include "got_compat.h"
26 2a00e21c 2021-09-28 thomas #include "xmalloc.h"
29 2a00e21c 2021-09-28 thomas xmalloc(size_t size)
31 2a00e21c 2021-09-28 thomas void *ptr;
33 2a00e21c 2021-09-28 thomas if (size == 0) {
34 2a00e21c 2021-09-28 thomas fprintf(stderr,"xmalloc: zero size");
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));
43 2a00e21c 2021-09-28 thomas return ptr;
47 2a00e21c 2021-09-28 thomas xcalloc(size_t nmemb, size_t size)
49 2a00e21c 2021-09-28 thomas void *ptr;
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));
59 2a00e21c 2021-09-28 thomas return ptr;
63 2a00e21c 2021-09-28 thomas xrealloc(void *ptr, size_t size)
65 2a00e21c 2021-09-28 thomas return xreallocarray(ptr, 1, size);
69 2a00e21c 2021-09-28 thomas xreallocarray(void *ptr, size_t nmemb, size_t size)
71 2a00e21c 2021-09-28 thomas void *new_ptr;
73 2a00e21c 2021-09-28 thomas if (nmemb == 0 || size == 0) {
74 2a00e21c 2021-09-28 thomas fprintf(stderr, "xreallocarray: zero size");
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));
83 2a00e21c 2021-09-28 thomas return new_ptr;
87 2a00e21c 2021-09-28 thomas xrecallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size)
89 2a00e21c 2021-09-28 thomas void *new_ptr;
91 2a00e21c 2021-09-28 thomas if (nmemb == 0 || size == 0) {
92 2a00e21c 2021-09-28 thomas fprintf(stderr,"xrecallocarray: zero size");
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));
101 2a00e21c 2021-09-28 thomas return new_ptr;
105 2a00e21c 2021-09-28 thomas xstrdup(const char *str)
107 2a00e21c 2021-09-28 thomas char *cp;
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);
113 2a00e21c 2021-09-28 thomas return cp;
117 2a00e21c 2021-09-28 thomas xstrndup(const char *str, size_t maxlen)
119 2a00e21c 2021-09-28 thomas char *cp;
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);
125 2a00e21c 2021-09-28 thomas return cp;
129 2a00e21c 2021-09-28 thomas xasprintf(char **ret, const char *fmt, ...)
131 2a00e21c 2021-09-28 thomas va_list ap;
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);
138 2a00e21c 2021-09-28 thomas return i;
142 2a00e21c 2021-09-28 thomas xvasprintf(char **ret, const char *fmt, va_list ap)
146 2a00e21c 2021-09-28 thomas i = vasprintf(ret, fmt, ap);
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);
153 2a00e21c 2021-09-28 thomas return i;
157 2a00e21c 2021-09-28 thomas xsnprintf(char *str, size_t len, const char *fmt, ...)
159 2a00e21c 2021-09-28 thomas va_list ap;
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);
166 2a00e21c 2021-09-28 thomas return i;
170 2a00e21c 2021-09-28 thomas xvsnprintf(char *str, size_t len, const char *fmt, va_list ap)
174 2a00e21c 2021-09-28 thomas if (len > INT_MAX)
175 2a00e21c 2021-09-28 thomas fprintf(stderr,"xsnprintf: len > INT_MAX");
177 2a00e21c 2021-09-28 thomas i = vsnprintf(str, len, fmt, ap);
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);
184 2a00e21c 2021-09-28 thomas return i;