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 fatalx 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 <errno.h>
18 #include <limits.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "got_compat.h"
26 #include "xmalloc.h"
28 void *
29 xmalloc(size_t size)
30 {
31 void *ptr;
33 if (size == 0) {
34 fprintf(stderr,"xmalloc: zero size");
35 exit (1);
36 }
37 ptr = malloc(size);
38 if (ptr == NULL) {
39 fprintf(stderr, "xmalloc: allocating %zu bytes: %s",
40 size, strerror(errno));
41 exit (1);
42 }
43 return ptr;
44 }
46 void *
47 xcalloc(size_t nmemb, size_t size)
48 {
49 void *ptr;
51 if (size == 0 || nmemb == 0)
52 fprintf(stderr,"xcalloc: zero size");
53 ptr = calloc(nmemb, size);
54 if (ptr == NULL) {
55 fprintf(stderr, "xcalloc: allocating %zu * %zu bytes: %s",
56 nmemb, size, strerror(errno));
57 exit (1);
58 }
59 return ptr;
60 }
62 void *
63 xrealloc(void *ptr, size_t size)
64 {
65 return xreallocarray(ptr, 1, size);
66 }
68 void *
69 xreallocarray(void *ptr, size_t nmemb, size_t size)
70 {
71 void *new_ptr;
73 if (nmemb == 0 || size == 0) {
74 fprintf(stderr, "xreallocarray: zero size");
75 exit (1);
76 }
77 new_ptr = reallocarray(ptr, nmemb, size);
78 if (new_ptr == NULL) {
79 fprintf(stderr, "xreallocarray: allocating %zu * %zu bytes: %s",
80 nmemb, size, strerror(errno));
81 exit (1);
82 }
83 return new_ptr;
84 }
86 void *
87 xrecallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size)
88 {
89 void *new_ptr;
91 if (nmemb == 0 || size == 0) {
92 fprintf(stderr,"xrecallocarray: zero size");
93 exit (1);
94 }
95 new_ptr = recallocarray(ptr, oldnmemb, nmemb, size);
96 if (new_ptr == NULL) {
97 fprintf(stderr,"xrecallocarray: allocating %zu * %zu bytes: %s",
98 nmemb, size, strerror(errno));
99 exit (1);
101 return new_ptr;
104 char *
105 xstrdup(const char *str)
107 char *cp;
109 if ((cp = strdup(str)) == NULL) {
110 fprintf(stderr,"xstrdup: %s", strerror(errno));
111 exit (1);
113 return cp;
116 char *
117 xstrndup(const char *str, size_t maxlen)
119 char *cp;
121 if ((cp = strndup(str, maxlen)) == NULL) {
122 fprintf(stderr,"xstrndup: %s", strerror(errno));
123 exit (1);
125 return cp;
128 int
129 xasprintf(char **ret, const char *fmt, ...)
131 va_list ap;
132 int i;
134 va_start(ap, fmt);
135 i = xvasprintf(ret, fmt, ap);
136 va_end(ap);
138 return i;
141 int
142 xvasprintf(char **ret, const char *fmt, va_list ap)
144 int i;
146 i = vasprintf(ret, fmt, ap);
148 if (i == -1) {
149 fprintf(stderr,"xasprintf: %s", strerror(errno));
150 exit (1);
153 return i;
156 int
157 xsnprintf(char *str, size_t len, const char *fmt, ...)
159 va_list ap;
160 int i;
162 va_start(ap, fmt);
163 i = xvsnprintf(str, len, fmt, ap);
164 va_end(ap);
166 return i;
169 int
170 xvsnprintf(char *str, size_t len, const char *fmt, va_list ap)
172 int i;
174 if (len > INT_MAX)
175 fprintf(stderr,"xsnprintf: len > INT_MAX");
177 i = vsnprintf(str, len, fmt, ap);
179 if (i < 0 || i >= (int)len) {
180 fprintf(stderr,"xsnprintf: overflow");
181 exit (1);
184 return i;