1 /* $OpenBSD: xmalloc.c,v 1.33 2016/02/15 09:47:49 dtucker 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 xstrdup(const char *str)
86 len = strlen(str) + 1;
88 strlcpy(cp, str, len);
93 xasprintf(char **ret, const char *fmt, ...)
99 i = vasprintf(ret, fmt, ap);
102 if (i < 0 || *ret == NULL)
103 fatal("xasprintf: could not allocate memory");