1 /* $OpenBSD: xmalloc.c,v 1.4 2019/06/28 05:44:09 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 fatal("xmalloc: zero size");
35 fatal("xmalloc: allocating %zu bytes", size);
40 xcalloc(size_t nmemb, size_t size)
44 if (size == 0 || nmemb == 0)
45 fatal("xcalloc: zero size");
46 ptr = calloc(nmemb, size);
48 fatal("xcalloc: allocating %zu * %zu bytes", nmemb, size);
53 xreallocarray(void *ptr, size_t nmemb, size_t size)
57 new_ptr = reallocarray(ptr, nmemb, size);
59 fatal("xreallocarray: allocating %zu * %zu bytes",
65 xrecallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size)
69 new_ptr = recallocarray(ptr, oldnmemb, nmemb, size);
71 fatal("xrecallocarray: allocating %zu * %zu bytes",
77 xstrdup(const char *str)
81 if ((cp = strdup(str)) == NULL)
87 xasprintf(char **ret, const char *fmt, ...)
93 i = vasprintf(ret, fmt, ap);