4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 * Versions of malloc and friends that check their results, and never return
8 * failure (they call fatalx if they encounter an error).
10 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
32 fatalx("xmalloc: zero size");
35 fatalx("xmalloc: allocating %zu bytes: %s",
36 size
, strerror(errno
));
41 xcalloc(size_t nmemb
, size_t size
)
45 if (size
== 0 || nmemb
== 0)
46 fatalx("xcalloc: zero size");
47 ptr
= calloc(nmemb
, size
);
49 fatalx("xcalloc: allocating %zu * %zu bytes: %s",
50 nmemb
, size
, strerror(errno
));
55 xrealloc(void *ptr
, size_t size
)
57 return xreallocarray(ptr
, 1, size
);
61 xreallocarray(void *ptr
, size_t nmemb
, size_t size
)
65 if (nmemb
== 0 || size
== 0)
66 fatalx("xreallocarray: zero size");
67 new_ptr
= reallocarray(ptr
, nmemb
, size
);
69 fatalx("xreallocarray: allocating %zu * %zu bytes: %s",
70 nmemb
, size
, strerror(errno
));
75 xrecallocarray(void *ptr
, size_t oldnmemb
, size_t nmemb
, size_t size
)
79 if (nmemb
== 0 || size
== 0)
80 fatalx("xrecallocarray: zero size");
81 new_ptr
= recallocarray(ptr
, oldnmemb
, nmemb
, size
);
83 fatalx("xrecallocarray: allocating %zu * %zu bytes: %s",
84 nmemb
, size
, strerror(errno
));
89 xstrdup(const char *str
)
93 if ((cp
= strdup(str
)) == NULL
)
94 fatalx("xstrdup: %s", strerror(errno
));
99 xstrndup(const char *str
, size_t maxlen
)
103 if ((cp
= strndup(str
, maxlen
)) == NULL
)
104 fatalx("xstrndup: %s", strerror(errno
));
109 xasprintf(char **ret
, const char *fmt
, ...)
115 i
= xvasprintf(ret
, fmt
, ap
);
122 xvasprintf(char **ret
, const char *fmt
, va_list ap
)
126 i
= vasprintf(ret
, fmt
, ap
);
129 fatalx("xasprintf: %s", strerror(errno
));
135 xsnprintf(char *str
, size_t len
, const char *fmt
, ...)
141 i
= xvsnprintf(str
, len
, fmt
, ap
);
148 xvsnprintf(char *str
, size_t len
, const char *fmt
, va_list ap
)
153 fatalx("xsnprintf: len > INT_MAX");
155 i
= vsnprintf(str
, len
, fmt
, ap
);
157 if (i
< 0 || i
>= (int)len
)
158 fatalx("xsnprintf: overflow");