2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Versions of malloc and friends that check their results, and never return
6 * failure (they call fatal if they encounter an error).
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
26 fatal("xmalloc: zero size");
29 fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
34 xcalloc(size_t nmemb, size_t size)
38 if (nmemb && size && SIZE_T_MAX / nmemb < size)
39 fatal("xcalloc: nmemb * size > SIZE_T_MAX");
40 if (size == 0 || nmemb == 0)
41 fatal("xcalloc: zero size");
42 ptr = calloc(nmemb, size);
44 fatal("xcalloc: out of memory (allocating %lu bytes)",
45 (u_long)(size * nmemb));
50 xrealloc(void *ptr, size_t nmemb, size_t size)
53 size_t new_size = nmemb * size;
55 if (nmemb && size && SIZE_T_MAX / nmemb < size)
56 fatal("xrealloc: nmemb * size > SIZE_T_MAX");
58 fatal("xrealloc: zero size");
60 new_ptr = malloc(new_size);
62 new_ptr = realloc(ptr, new_size);
64 fatal("xrealloc: out of memory (new_size %lu bytes)",
73 fatal("xfree: NULL pointer given as argument");
78 xstrdup(const char *str)
83 len = strlen(str) + 1;
85 strlcpy(cp, str, len);
90 xasprintf(char **ret, const char *fmt, ...)
96 i = vasprintf(ret, fmt, ap);
99 if (i < 0 || *ret == NULL)
100 fatal("xasprintf: could not allocate memory");