From 371275cabfe7ee71db4f4e23b9a5d966b335d3bc Mon Sep 17 00:00:00 2001 From: deraadt Date: Wed, 29 Apr 2015 04:00:25 +0000 Subject: [PATCH] Change internal xrealloc() to a idiom-following xreallocarray(). This loses a "new size is 0" failure case. Probably not relevant; and since we develop this in OpenBSD, we'll catch that before someone else imports this... ok millert --- usr.bin/diff/diff.c | 4 ++-- usr.bin/diff/diffreg.c | 18 +++++++++--------- usr.bin/diff/xmalloc.c | 14 +++----------- usr.bin/diff/xmalloc.h | 4 ++-- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c index fbe46714f7d..743e9332f36 100644 --- a/usr.bin/diff/diff.c +++ b/usr.bin/diff/diff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diff.c,v 1.58 2015/01/16 06:40:07 deraadt Exp $ */ +/* $OpenBSD: diff.c,v 1.59 2015/04/29 04:00:25 deraadt Exp $ */ /* * Copyright (c) 2003 Todd C. Miller @@ -337,7 +337,7 @@ push_ignore_pats(char *pattern) else { /* old + "|" + new + NUL */ len = strlen(ignore_pats) + strlen(pattern) + 2; - ignore_pats = xrealloc(ignore_pats, 1, len); + ignore_pats = xreallocarray(ignore_pats, 1, len); strlcat(ignore_pats, "|", len); strlcat(ignore_pats, pattern, len); } diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c index e4eaa00f49c..60eaec0209a 100644 --- a/usr.bin/diff/diffreg.c +++ b/usr.bin/diff/diffreg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diffreg.c,v 1.85 2015/02/05 12:59:57 millert Exp $ */ +/* $OpenBSD: diffreg.c,v 1.86 2015/04/29 04:00:25 deraadt Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -418,11 +418,11 @@ diffreg(char *file1, char *file2, int flags) member = (int *)file[1]; equiv(sfile[0], slen[0], sfile[1], slen[1], member); - member = xrealloc(member, slen[1] + 2, sizeof(*member)); + member = xreallocarray(member, slen[1] + 2, sizeof(*member)); class = (int *)file[0]; unsort(sfile[0], slen[0], class); - class = xrealloc(class, slen[0] + 2, sizeof(*class)); + class = xreallocarray(class, slen[0] + 2, sizeof(*class)); klist = xcalloc(slen[0] + 2, sizeof(*klist)); clen = 0; @@ -432,13 +432,13 @@ diffreg(char *file1, char *file2, int flags) xfree(member); xfree(class); - J = xrealloc(J, len[0] + 2, sizeof(*J)); + J = xreallocarray(J, len[0] + 2, sizeof(*J)); unravel(klist[i]); xfree(clist); xfree(klist); - ixold = xrealloc(ixold, len[0] + 2, sizeof(*ixold)); - ixnew = xrealloc(ixnew, len[1] + 2, sizeof(*ixnew)); + ixold = xreallocarray(ixold, len[0] + 2, sizeof(*ixold)); + ixnew = xreallocarray(ixnew, len[1] + 2, sizeof(*ixnew)); check(f1, f2, flags); output(file1, f1, file2, f2, flags); if (ostdout != -1) { @@ -570,7 +570,7 @@ prepare(int i, FILE *fd, off_t filesize, int flags) for (j = 0; (h = readhash(fd, flags));) { if (j == sz) { sz = sz * 3 / 2; - p = xrealloc(p, sz + 3, sizeof(*p)); + p = xreallocarray(p, sz + 3, sizeof(*p)); } p[++j].value = h; } @@ -701,7 +701,7 @@ newcand(int x, int y, int pred) if (clen == clistlen) { clistlen = clistlen * 11 / 10; - clist = xrealloc(clist, clistlen, sizeof(*clist)); + clist = xreallocarray(clist, clistlen, sizeof(*clist)); } q = clist + clen; q->x = x; @@ -1064,7 +1064,7 @@ proceed: if (context_vec_ptr == context_vec_end - 1) { ptrdiff_t offset = context_vec_ptr - context_vec_start; max_context <<= 1; - context_vec_start = xrealloc(context_vec_start, + context_vec_start = xreallocarray(context_vec_start, max_context, sizeof(*context_vec_start)); context_vec_end = context_vec_start + max_context; context_vec_ptr = context_vec_start + offset; diff --git a/usr.bin/diff/xmalloc.c b/usr.bin/diff/xmalloc.c index 767500c0b07..2eccb0fd63a 100644 --- a/usr.bin/diff/xmalloc.c +++ b/usr.bin/diff/xmalloc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: xmalloc.c,v 1.5 2015/02/05 12:59:57 millert Exp $ */ +/* $OpenBSD: xmalloc.c,v 1.6 2015/04/29 04:00:25 deraadt Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -52,19 +52,11 @@ xcalloc(size_t nmemb, size_t size) } void * -xrealloc(void *ptr, size_t nmemb, size_t size) +xreallocarray(void *ptr, size_t nmemb, size_t size) { void *new_ptr; - size_t new_size = nmemb * size; - if (new_size == 0) - errx(2, "xrealloc: zero size"); - if (SIZE_MAX / nmemb < size) - errx(2, "xrealloc: nmemb * size > SIZE_MAX"); - if (ptr == NULL) - new_ptr = malloc(new_size); - else - new_ptr = realloc(ptr, new_size); + new_ptr = reallocarray(ptr, nmemb, size); if (new_ptr == NULL) err(2, NULL); return new_ptr; diff --git a/usr.bin/diff/xmalloc.h b/usr.bin/diff/xmalloc.h index f348ba23c33..d4aa769f552 100644 --- a/usr.bin/diff/xmalloc.h +++ b/usr.bin/diff/xmalloc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: xmalloc.h,v 1.2 2009/06/07 08:39:13 ray Exp $ */ +/* $OpenBSD: xmalloc.h,v 1.3 2015/04/29 04:00:25 deraadt Exp $ */ /* * Author: Tatu Ylonen @@ -21,7 +21,7 @@ void *xmalloc(size_t); void *xcalloc(size_t, size_t); -void *xrealloc(void *, size_t, size_t); +void *xreallocarray(void *, size_t, size_t); void xfree(void *); char *xstrdup(const char *); int xasprintf(char **, const char *, ...) -- 2.20.1