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".
34 fatal("xmalloc: zero size");
37 fatal("xmalloc: allocating %zu bytes", size);
42 xcalloc(size_t nmemb, size_t size)
46 if (size == 0 || nmemb == 0)
47 fatal("xcalloc: zero size");
48 ptr = calloc(nmemb, size);
50 fatal("xcalloc: allocating %zu * %zu bytes", nmemb, size);
55 xreallocarray(void *ptr, size_t nmemb, size_t size)
59 new_ptr = reallocarray(ptr, nmemb, size);
61 fatal("xreallocarray: allocating %zu * %zu bytes",
67 xrecallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size)
71 new_ptr = recallocarray(ptr, oldnmemb, nmemb, size);
73 fatal("xrecallocarray: allocating %zu * %zu bytes",
79 xstrdup(const char *str)
83 if ((cp = strdup(str)) == NULL)
89 xasprintf(char **ret, const char *fmt, ...)
95 i = vasprintf(ret, fmt, ap);