summaryrefslogtreecommitdiff
path: root/xmalloc.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2006-03-26 14:19:21 +1100
committerDamien Miller <djm@mindrot.org>2006-03-26 14:19:21 +1100
commit07d86bec5eeaf19fe33dca99c8ebcbe9a77c3938 (patch)
tree098295eee2d7ec7b116b0db3ac4b580713dd5ab0 /xmalloc.c
parent7cd4579eb3c5afd22ae24436fd2611cd3aa0150a (diff)
- djm@cvs.openbsd.org 2006/03/25 00:05:41
[auth-bsdauth.c auth-skey.c auth.c auth2-chall.c channels.c] [clientloop.c deattack.c gss-genr.c kex.c key.c misc.c moduli.c] [monitor.c monitor_wrap.c packet.c scard.c sftp-server.c ssh-agent.c] [ssh-keyscan.c ssh.c sshconnect.c sshconnect2.c sshd.c uuencode.c] [xmalloc.c xmalloc.h] introduce xcalloc() and xasprintf() failure-checked allocations functions and use them throughout openssh xcalloc is particularly important because malloc(nmemb * size) is a dangerous idiom (subject to integer overflow) and it is time for it to die feedback and ok deraadt@
Diffstat (limited to 'xmalloc.c')
-rw-r--r--xmalloc.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/xmalloc.c b/xmalloc.c
index 64e439853..6d56781d9 100644
--- a/xmalloc.c
+++ b/xmalloc.c
@@ -31,6 +31,22 @@ xmalloc(size_t size)
31} 31}
32 32
33void * 33void *
34xcalloc(size_t nmemb, size_t size)
35{
36 void *ptr;
37
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);
43 if (ptr == NULL)
44 fatal("xcalloc: out of memory (allocating %lu bytes)",
45 (u_long)(size * nmemb));
46 return ptr;
47}
48
49void *
34xrealloc(void *ptr, size_t new_size) 50xrealloc(void *ptr, size_t new_size)
35{ 51{
36 void *new_ptr; 52 void *new_ptr;
@@ -65,3 +81,19 @@ xstrdup(const char *str)
65 strlcpy(cp, str, len); 81 strlcpy(cp, str, len);
66 return cp; 82 return cp;
67} 83}
84
85int
86xasprintf(char **ret, const char *fmt, ...)
87{
88 va_list ap;
89 int i;
90
91 va_start(ap, fmt);
92 i = vasprintf(ret, fmt, ap);
93 va_end(ap);
94
95 if (i < 0 || *ret == NULL)
96 fatal("xasprintf: could not allocate memory");
97
98 return (i);
99}