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".
31 errx(2, "xmalloc: zero size");
34 err(2, "xmalloc: allocating %zu bytes", size);
39 xcalloc(size_t nmemb, size_t size)
43 ptr = calloc(nmemb, size);
45 err(2, "xcalloc: allocating %zu * %zu bytes", nmemb, size);
50 xreallocarray(void *ptr, size_t nmemb, size_t size)
54 new_ptr = reallocarray(ptr, nmemb, size);
56 err(2, "xreallocarray: allocating %zu * %zu bytes",
62 xstrdup(const char *str)
66 if ((cp = strdup(str)) == NULL)
72 xasprintf(char **ret, const char *fmt, ...)
78 i = vasprintf(ret, fmt, ap);
81 if (i < 0 || *ret == NULL)