summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--usr.bin/cvs/buf.c4
-rw-r--r--usr.bin/cvs/diff.c18
-rw-r--r--usr.bin/cvs/diff3.c14
-rw-r--r--usr.bin/cvs/rcs.c6
-rw-r--r--usr.bin/cvs/rcsnum.c10
-rw-r--r--usr.bin/cvs/util.c4
-rw-r--r--usr.bin/cvs/xmalloc.c46
-rw-r--r--usr.bin/cvs/xmalloc.h10
8 files changed, 75 insertions, 37 deletions
diff --git a/usr.bin/cvs/buf.c b/usr.bin/cvs/buf.c
index 29c7bc1c85f..c2e0a98a6cb 100644
--- a/usr.bin/cvs/buf.c
+++ b/usr.bin/cvs/buf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buf.c,v 1.40 2006/03/25 21:29:58 ray Exp $ */
+/* $OpenBSD: buf.c,v 1.41 2006/03/28 02:13:44 ray Exp $ */
/*
* Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
@@ -435,7 +435,7 @@ cvs_buf_grow(BUF *b, size_t len)
if (b->cb_size == 0)
tmp = xmalloc(len);
else
- tmp = xrealloc(b->cb_buf, b->cb_size + len);
+ tmp = xrealloc(b->cb_buf, 1, b->cb_size + len);
b->cb_buf = (u_char *)tmp;
b->cb_size += len;
diff --git a/usr.bin/cvs/diff.c b/usr.bin/cvs/diff.c
index f4f6d2d935c..c7579a0515e 100644
--- a/usr.bin/cvs/diff.c
+++ b/usr.bin/cvs/diff.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: diff.c,v 1.83 2006/03/25 21:29:59 ray Exp $ */
+/* $OpenBSD: diff.c,v 1.84 2006/03/28 02:13:44 ray Exp $ */
/*
* Copyright (C) Caldera International Inc. 2001-2002.
* All rights reserved.
@@ -713,12 +713,12 @@ cvs_diffreg(const char *file1, const char *file2, BUF *out)
member = (int *)file[1];
equiv(sfile[0], slen[0], sfile[1], slen[1], member);
- tmp = xrealloc(member, (slen[1] + 2) * sizeof(int));
+ tmp = xrealloc(member, slen[1] + 2, sizeof(int));
member = (int *)tmp;
class = (int *)file[0];
unsort(sfile[0], slen[0], class);
- tmp = xrealloc(class, (slen[0] + 2) * sizeof(int));
+ tmp = xrealloc(class, slen[0] + 2, sizeof(int));
class = (int *)tmp;
klist = xmalloc((slen[0] + 2) * sizeof(int));
@@ -732,16 +732,16 @@ cvs_diffreg(const char *file1, const char *file2, BUF *out)
xfree(member);
xfree(class);
- tmp = xrealloc(J, (diff_len[0] + 2) * sizeof(int));
+ tmp = xrealloc(J, diff_len[0] + 2, sizeof(int));
J = (int *)tmp;
unravel(klist[i]);
xfree(clist);
xfree(klist);
- tmp = xrealloc(ixold, (diff_len[0] + 2) * sizeof(long));
+ tmp = xrealloc(ixold, diff_len[0] + 2, sizeof(long));
ixold = (long *)tmp;
- tmp = xrealloc(ixnew, (diff_len[1] + 2) * sizeof(long));
+ tmp = xrealloc(ixnew, diff_len[1] + 2, sizeof(long));
ixnew = (long *)tmp;
check(f1, f2);
output(f1, f2);
@@ -805,7 +805,7 @@ prepare(int i, FILE *fd, off_t filesize)
for (j = 0; (h = readhash(fd));) {
if (j == (int)sz) {
sz = sz * 3 / 2;
- tmp = xrealloc(p, (sz + 3) * sizeof(struct line));
+ tmp = xrealloc(p, sz + 3, sizeof(struct line));
p = (struct line *)tmp;
}
p[++j].value = h;
@@ -945,7 +945,7 @@ newcand(int x, int y, int pred)
if (clen == clistlen) {
newclistlen = clistlen * 11 / 10;
- tmp = xrealloc(clist, newclistlen * sizeof(cand));
+ tmp = xrealloc(clist, newclistlen, sizeof(cand));
clist = tmp;
clistlen = newclistlen;
}
@@ -1286,7 +1286,7 @@ proceed:
struct context_vec *tmp;
ptrdiff_t offset = context_vec_ptr - context_vec_start;
max_context <<= 1;
- tmp = xrealloc(context_vec_start, max_context *
+ tmp = xrealloc(context_vec_start, max_context,
sizeof(struct context_vec));
context_vec_start = tmp;
context_vec_end = context_vec_start + max_context;
diff --git a/usr.bin/cvs/diff3.c b/usr.bin/cvs/diff3.c
index 128a6782188..9a3fe391f03 100644
--- a/usr.bin/cvs/diff3.c
+++ b/usr.bin/cvs/diff3.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: diff3.c,v 1.20 2006/03/25 21:29:59 ray Exp $ */
+/* $OpenBSD: diff3.c,v 1.21 2006/03/28 02:13:44 ray Exp $ */
/*
* Copyright (C) Caldera International Inc. 2001-2002.
@@ -72,7 +72,7 @@ static const char copyright[] =
#ifndef lint
static const char rcsid[] =
- "$OpenBSD: diff3.c,v 1.20 2006/03/25 21:29:59 ray Exp $";
+ "$OpenBSD: diff3.c,v 1.21 2006/03/28 02:13:44 ray Exp $";
#endif /* not lint */
#include "includes.h"
@@ -488,7 +488,7 @@ getline(FILE *b, size_t *n)
do {
bufsize += 1024;
} while (len + 1 > bufsize);
- buf = xrealloc(buf, bufsize);
+ buf = xrealloc(buf, 1, bufsize);
}
memcpy(buf, cp, len - 1);
buf[len - 1] = '\n';
@@ -788,16 +788,16 @@ increase(void)
newsz = szchanges == 0 ? 64 : 2 * szchanges;
incr = newsz - szchanges;
- p = xrealloc(d13, newsz * sizeof(struct diff));
+ p = xrealloc(d13, newsz, sizeof(struct diff));
memset(p + szchanges, 0, incr * sizeof(struct diff));
d13 = p;
- p = xrealloc(d23, newsz * sizeof(struct diff));
+ p = xrealloc(d23, newsz, sizeof(struct diff));
memset(p + szchanges, 0, incr * sizeof(struct diff));
d23 = p;
- p = xrealloc(de, newsz * sizeof(struct diff));
+ p = xrealloc(de, newsz, sizeof(struct diff));
memset(p + szchanges, 0, incr * sizeof(struct diff));
de = p;
- q = xrealloc(overlap, newsz * sizeof(char));
+ q = xrealloc(overlap, newsz, sizeof(char));
memset(q + szchanges, 0, incr * sizeof(char));
overlap = q;
szchanges = newsz;
diff --git a/usr.bin/cvs/rcs.c b/usr.bin/cvs/rcs.c
index 5e0a65551f7..179535860c6 100644
--- a/usr.bin/cvs/rcs.c
+++ b/usr.bin/cvs/rcs.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rcs.c,v 1.152 2006/03/27 21:56:32 niallo Exp $ */
+/* $OpenBSD: rcs.c,v 1.153 2006/03/28 02:13:44 ray Exp $ */
/*
* Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
@@ -2432,7 +2432,7 @@ rcs_growbuf(RCSFILE *rf)
void *tmp;
struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;
- tmp = xrealloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
+ tmp = xrealloc(pdp->rp_buf, 1, pdp->rp_blen + RCS_BUFEXTSIZE);
pdp->rp_buf = (char *)tmp;
pdp->rp_blen += RCS_BUFEXTSIZE;
pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;
@@ -2641,7 +2641,7 @@ rcs_expand_keywords(char *rcsfile, struct rcs_delta *rdp, char *data,
/* only realloc if we have to */
if (sizdiff > 0) {
len += sizdiff;
- data = xrealloc(data, len);
+ data = xrealloc(data, 1, len);
/*
* ensure string pointers are not invalidated
* after realloc()
diff --git a/usr.bin/cvs/rcsnum.c b/usr.bin/cvs/rcsnum.c
index 86f8b1811a7..5431357e358 100644
--- a/usr.bin/cvs/rcsnum.c
+++ b/usr.bin/cvs/rcsnum.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rcsnum.c,v 1.28 2006/03/18 03:33:55 ray Exp $ */
+/* $OpenBSD: rcsnum.c,v 1.29 2006/03/28 02:13:44 ray Exp $ */
/*
* Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
@@ -156,7 +156,7 @@ rcsnum_cpy(const RCSNUM *nsrc, RCSNUM *ndst, u_int depth)
len = depth;
sz = len * sizeof(u_int16_t);
- tmp = xrealloc(ndst->rn_id, sz);
+ tmp = xrealloc(ndst->rn_id, 1, sz);
ndst->rn_id = (u_int16_t *)tmp;
ndst->rn_len = len;
memcpy(ndst->rn_id, nsrc->rn_id, sz);
@@ -234,7 +234,7 @@ rcsnum_aton(const char *str, char **ep, RCSNUM *nump)
nump->rn_len++;
tmp = xrealloc(nump->rn_id,
- (nump->rn_len + 1) * sizeof(u_int16_t));
+ nump->rn_len + 1, sizeof(u_int16_t));
nump->rn_id = (u_int16_t *)tmp;
nump->rn_id[nump->rn_len] = 0;
continue;
@@ -301,7 +301,7 @@ rcsnum_aton(const char *str, char **ep, RCSNUM *nump)
/* We can't have a single-digit rcs number. */
if (nump->rn_len == 0) {
tmp = xrealloc(nump->rn_id,
- (nump->rn_len + 1) * sizeof(u_int16_t));
+ nump->rn_len + 1, sizeof(u_int16_t));
nump->rn_id = (u_int16_t *)tmp;
nump->rn_id[nump->rn_len + 1] = 0;
nump->rn_len++;
@@ -404,7 +404,7 @@ rcsnum_setsize(RCSNUM *num, u_int len)
{
void *tmp;
- tmp = xrealloc(num->rn_id, len * sizeof(u_int16_t));
+ tmp = xrealloc(num->rn_id, len, sizeof(u_int16_t));
num->rn_id = (u_int16_t *)tmp;
num->rn_len = len;
return (0);
diff --git a/usr.bin/cvs/util.c b/usr.bin/cvs/util.c
index 6b68bb3b7c8..a11d84bc0c3 100644
--- a/usr.bin/cvs/util.c
+++ b/usr.bin/cvs/util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.73 2006/03/27 06:13:51 pat Exp $ */
+/* $OpenBSD: util.c,v 1.74 2006/03/28 02:13:44 ray Exp $ */
/*
* Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
* Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org>
@@ -1068,7 +1068,7 @@ cvs_strsplit(char *str, const char *sep)
while ((p = strsep(&cp, sep)) != NULL) {
av->argv[i++] = p;
nargv = (char **)xrealloc((void *)av->argv,
- (i+1) * sizeof(char *));
+ i + 1, sizeof(char *));
av->argv = nargv;
}
av->argv[i] = NULL;
diff --git a/usr.bin/cvs/xmalloc.c b/usr.bin/cvs/xmalloc.c
index e4182a21d03..1baf4a1ea2d 100644
--- a/usr.bin/cvs/xmalloc.c
+++ b/usr.bin/cvs/xmalloc.c
@@ -1,9 +1,9 @@
-/* $OpenBSD: xmalloc.c,v 1.5 2006/01/02 08:11:56 xsa Exp $ */
+/* $OpenBSD: xmalloc.c,v 1.6 2006/03/28 02:13:44 ray Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
* All rights reserved
- * Versions of xmalloc and friends that check their results, and never return
+ * Versions of malloc and friends that check their results, and never return
* failure (they call fatal if they encounter an error).
*
* As far as I am concerned, the code I have written for this software
@@ -27,20 +27,38 @@ xmalloc(size_t size)
fatal("xmalloc: zero size");
ptr = malloc(size);
if (ptr == NULL)
- fatal("xmalloc: out of memory (allocating %lu bytes)",
- (u_long) size);
+ fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
return ptr;
}
void *
-xrealloc(void *ptr, size_t new_size)
+xcalloc(size_t nmemb, size_t size)
+{
+ void *ptr;
+
+ if (size == 0 || nmemb == 0)
+ fatal("xcalloc: zero size");
+ if (SIZE_T_MAX / nmemb < size)
+ fatal("xcalloc: nmemb * size > SIZE_T_MAX");
+ ptr = calloc(nmemb, size);
+ if (ptr == NULL)
+ fatal("xcalloc: out of memory (allocating %lu bytes)",
+ (u_long)(size * nmemb));
+ return ptr;
+}
+
+void *
+xrealloc(void *ptr, size_t nmemb, size_t size)
{
void *new_ptr;
+ size_t new_size = nmemb * size;
if (new_size == 0)
fatal("xrealloc: zero size");
+ if (SIZE_T_MAX / nmemb < size)
+ fatal("xrealloc: nmemb * size > SIZE_T_MAX");
if (ptr == NULL)
- new_ptr = xmalloc(new_size);
+ new_ptr = malloc(new_size);
else
new_ptr = realloc(ptr, new_size);
if (new_ptr == NULL)
@@ -68,3 +86,19 @@ xstrdup(const char *str)
strlcpy(cp, str, len);
return cp;
}
+
+int
+xasprintf(char **ret, const char *fmt, ...)
+{
+ va_list ap;
+ int i;
+
+ va_start(ap, fmt);
+ i = vasprintf(ret, fmt, ap);
+ va_end(ap);
+
+ if (i < 0 || *ret == NULL)
+ fatal("xasprintf: could not allocate memory");
+
+ return (i);
+}
diff --git a/usr.bin/cvs/xmalloc.h b/usr.bin/cvs/xmalloc.h
index b40cddd7fc5..736666950d2 100644
--- a/usr.bin/cvs/xmalloc.h
+++ b/usr.bin/cvs/xmalloc.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: xmalloc.h,v 1.1 2005/12/10 20:27:45 joris Exp $ */
+/* $OpenBSD: xmalloc.h,v 1.2 2006/03/28 02:13:44 ray Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -6,7 +6,7 @@
* All rights reserved
* Created: Mon Mar 20 22:09:17 1995 ylo
*
- * Versions of xmalloc and friends that check their results, and never return
+ * Versions of malloc and friends that check their results, and never return
* failure (they call fatal if they encounter an error).
*
* As far as I am concerned, the code I have written for this software
@@ -20,8 +20,12 @@
#define XMALLOC_H
void *xmalloc(size_t);
-void *xrealloc(void *, size_t);
+void *xcalloc(size_t, size_t);
+void *xrealloc(void *, size_t, size_t);
void xfree(void *);
char *xstrdup(const char *);
+int xasprintf(char **, const char *, ...)
+ __attribute__((__format__ (printf, 2, 3)))
+ __attribute__((__nonnull__ (2)));
#endif /* XMALLOC_H */