sync files from diff.git 346d4986568ffbcfdc53cce45dd8a468535a68f2
[got-portable.git] / compat / xmalloc.c
blob4cb3fee954c0994f90c6ad916cce9abc291f8614
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).
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".
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)
31 void *ptr;
33 if (size == 0) {
34 fprintf(stderr,"xmalloc: zero size");
35 exit (1);
37 ptr = malloc(size);
38 if (ptr == NULL) {
39 fprintf(stderr, "xmalloc: allocating %zu bytes: %s",
40 size, strerror(errno));
41 exit (1);
43 return ptr;
46 void *
47 xcalloc(size_t nmemb, size_t size)
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);
59 return ptr;
62 void *
63 xrealloc(void *ptr, size_t size)
65 return xreallocarray(ptr, 1, size);
68 void *
69 xreallocarray(void *ptr, size_t nmemb, size_t size)
71 void *new_ptr;
73 if (nmemb == 0 || size == 0) {
74 fprintf(stderr, "xreallocarray: zero size");
75 exit (1);
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);
83 return new_ptr;
86 void *
87 xrecallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size)
89 void *new_ptr;
91 if (nmemb == 0 || size == 0) {
92 fprintf(stderr,"xrecallocarray: zero size");
93 exit (1);
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;
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;
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;
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;
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;