1 /* $OpenBSD: xmalloc.c,v 1.37 2022/03/13 23:27:54 cheloha 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".
29 #if defined(__OpenBSD__)
30 char *malloc_options
= "S";
31 #endif /* __OpenBSD__ */
39 fatal("xmalloc: zero size");
42 fatal("xmalloc: out of memory (allocating %zu bytes)", size
);
47 xcalloc(size_t nmemb
, size_t size
)
51 if (size
== 0 || nmemb
== 0)
52 fatal("xcalloc: zero size");
53 if (SIZE_MAX
/ nmemb
< size
)
54 fatal("xcalloc: nmemb * size > SIZE_MAX");
55 ptr
= calloc(nmemb
, size
);
57 fatal("xcalloc: out of memory (allocating %zu bytes)",
63 xreallocarray(void *ptr
, size_t nmemb
, size_t size
)
67 new_ptr
= reallocarray(ptr
, nmemb
, size
);
69 fatal("xreallocarray: out of memory (%zu elements of %zu bytes)",
75 xrecallocarray(void *ptr
, size_t onmemb
, size_t nmemb
, size_t size
)
79 new_ptr
= recallocarray(ptr
, onmemb
, nmemb
, size
);
81 fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)",
87 xstrdup(const char *str
)
92 len
= strlen(str
) + 1;
94 return memcpy(cp
, str
, len
);
98 xvasprintf(char **ret
, const char *fmt
, va_list ap
)
102 i
= vasprintf(ret
, fmt
, ap
);
103 if (i
< 0 || *ret
== NULL
)
104 fatal("xvasprintf: could not allocate memory");
109 xasprintf(char **ret
, const char *fmt
, ...)
115 i
= xvasprintf(ret
, fmt
, ap
);