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