1 /* $OpenBSD: xmalloc.c,v 1.9 2015/11/17 18:25:02 tobias 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".
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
34 errx(2, "xmalloc: zero size");
37 err(2, "xmalloc: allocating %zu bytes", size);
42 xcalloc(size_t nmemb, size_t size)
46 ptr = calloc(nmemb, size);
48 err(2, "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 err(2, "xreallocarray: allocating %zu * %zu bytes",
65 xstrdup(const char *str)
69 if ((cp = strdup(str)) == NULL)
75 xasprintf(char **ret, const char *fmt, ...)
81 i = vasprintf(ret, fmt, ap);
84 if (i < 0 || *ret == NULL)