1 /* $OpenBSD: xmalloc.c,v 1.34 2017/05/31 09:15:42 deraadt Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Versions of malloc and friends that check their results, and never return
7 * failure (they call fatal if they encounter an error).
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
32 #if defined(__OpenBSD__)
33 extern char *malloc_options
;
36 #endif /* __OpenBSD__ */
45 fatal("xmalloc: zero size");
48 fatal("xmalloc: out of memory (allocating %zu bytes)", size
);
53 xcalloc(size_t nmemb
, size_t size
)
57 if (size
== 0 || nmemb
== 0)
58 fatal("xcalloc: zero size");
59 if (SIZE_MAX
/ nmemb
< size
)
60 fatal("xcalloc: nmemb * size > SIZE_MAX");
61 ptr
= calloc(nmemb
, size
);
63 fatal("xcalloc: out of memory (allocating %zu bytes)",
69 xreallocarray(void *ptr
, size_t nmemb
, size_t size
)
73 new_ptr
= reallocarray(ptr
, nmemb
, size
);
75 fatal("xreallocarray: out of memory (%zu elements of %zu bytes)",
81 xrecallocarray(void *ptr
, size_t onmemb
, size_t nmemb
, size_t size
)
85 new_ptr
= recallocarray(ptr
, onmemb
, nmemb
, size
);
87 fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)",
93 xstrdup(const char *str
)
98 len
= strlen(str
) + 1;
100 strlcpy(cp
, str
, len
);
105 xasprintf(char **ret
, const char *fmt
, ...)
111 i
= vasprintf(ret
, fmt
, ap
);
114 if (i
< 0 || *ret
== NULL
)
115 fatal("xasprintf: could not allocate memory");