summaryrefslogtreecommitdiff
path: root/usr.bin/file
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>2014-05-18 17:50:12 +0000
committerMarc Espie <espie@cvs.openbsd.org>2014-05-18 17:50:12 +0000
commit69f8c22e8c6f07e305a99b270327443dceffcb6a (patch)
tree454aa248b7980343e04467696f41a2a5b66be8e7 /usr.bin/file
parent0ee767acec5ac14aada0673c011486d1b84c2e8a (diff)
use reallocarray
okay ian@, chl@
Diffstat (limited to 'usr.bin/file')
-rw-r--r--usr.bin/file/apprentice.c23
-rw-r--r--usr.bin/file/ascmagic.c6
-rw-r--r--usr.bin/file/file.h5
-rw-r--r--usr.bin/file/funcs.c7
4 files changed, 24 insertions, 17 deletions
diff --git a/usr.bin/file/apprentice.c b/usr.bin/file/apprentice.c
index 90ba8398693..f5d0b6be3da 100644
--- a/usr.bin/file/apprentice.c
+++ b/usr.bin/file/apprentice.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: apprentice.c,v 1.30 2014/01/16 21:45:33 tobias Exp $ */
+/* $OpenBSD: apprentice.c,v 1.31 2014/05/18 17:50:11 espie Exp $ */
/*
* Copyright (c) Ian F. Darwin 1986-1995.
* Software written by Ian F. Darwin and others;
@@ -629,7 +629,7 @@ apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
maxmagic = MAXMAGIS;
if ((marray = calloc(maxmagic, sizeof(*marray))) == NULL) {
- file_oomem(ms, maxmagic * sizeof(*marray));
+ file_oomem2(ms, maxmagic, sizeof(*marray));
return -1;
}
marraycount = 0;
@@ -714,8 +714,8 @@ apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
for (i = 0; i < marraycount; i++)
mentrycount += marray[i].cont_count;
- if ((*magicp = malloc(sizeof(**magicp) * mentrycount)) == NULL) {
- file_oomem(ms, sizeof(**magicp) * mentrycount);
+ if ((*magicp = reallocarray(NULL, mentrycount, sizeof(**magicp))) == NULL) {
+ file_oomem2(ms, mentrycount, sizeof(**magicp));
errs++;
goto out;
}
@@ -1003,8 +1003,9 @@ parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
if (me->cont_count == me->max_count) {
struct magic *nm;
size_t cnt = me->max_count + ALLOC_CHUNK;
- if ((nm = realloc(me->mp, sizeof(*nm) * cnt)) == NULL) {
- file_oomem(ms, sizeof(*nm) * cnt);
+ if ((nm = reallocarray(me->mp, cnt, sizeof(*nm)))
+ == NULL) {
+ file_oomem2(ms, cnt, sizeof(*nm));
return -1;
}
me->mp = m = nm;
@@ -1018,9 +1019,9 @@ parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
struct magic_entry *mp;
maxmagic += ALLOC_INCR;
- if ((mp = realloc(*mentryp, sizeof(*mp) * maxmagic)) ==
- NULL) {
- file_oomem(ms, sizeof(*mp) * maxmagic);
+ if ((mp = reallocarray(*mentryp, maxmagic,
+ sizeof(*mp))) == NULL) {
+ file_oomem2(ms, maxmagic, sizeof(*mp));
return -1;
}
(void)memset(&mp[*nmentryp], 0, sizeof(*mp) *
@@ -1029,8 +1030,8 @@ parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
}
me = &(*mentryp)[*nmentryp];
if (me->mp == NULL) {
- if ((m = malloc(sizeof(*m) * ALLOC_CHUNK)) == NULL) {
- file_oomem(ms, sizeof(*m) * ALLOC_CHUNK);
+ if ((m = reallocarray(NULL, ALLOC_CHUNK, sizeof(*m))) == NULL) {
+ file_oomem2(ms, ALLOC_CHUNK, sizeof(*m));
return -1;
}
me->mp = m;
diff --git a/usr.bin/file/ascmagic.c b/usr.bin/file/ascmagic.c
index abb785e6aaf..a5d09dc4931 100644
--- a/usr.bin/file/ascmagic.c
+++ b/usr.bin/file/ascmagic.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ascmagic.c,v 1.11 2009/10/27 23:59:37 deraadt Exp $ */
+/* $OpenBSD: ascmagic.c,v 1.12 2014/05/18 17:50:11 espie Exp $ */
/*
* Copyright (c) Ian F. Darwin 1986-1995.
* Software written by Ian F. Darwin and others;
@@ -101,9 +101,9 @@ file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
while (nbytes > 1 && buf[nbytes - 1] == '\0')
nbytes--;
- if ((nbuf = calloc(1, (nbytes + 1) * sizeof(nbuf[0]))) == NULL)
+ if ((nbuf = calloc((nbytes + 1), sizeof(nbuf[0]))) == NULL)
goto done;
- if ((ubuf = calloc(1, (nbytes + 1) * sizeof(ubuf[0]))) == NULL)
+ if ((ubuf = calloc((nbytes + 1), sizeof(ubuf[0]))) == NULL)
goto done;
/*
diff --git a/usr.bin/file/file.h b/usr.bin/file/file.h
index 40d296ba62d..27229b1a6c3 100644
--- a/usr.bin/file/file.h
+++ b/usr.bin/file/file.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: file.h,v 1.23 2013/04/17 15:01:26 deraadt Exp $ */
+/* $OpenBSD: file.h,v 1.24 2014/05/18 17:50:11 espie Exp $ */
/*
* Copyright (c) Ian F. Darwin 1986-1995.
* Software written by Ian F. Darwin and others;
@@ -28,7 +28,7 @@
*/
/*
* file.h - definitions for file(1) program
- * @(#)$Id: file.h,v 1.23 2013/04/17 15:01:26 deraadt Exp $
+ * @(#)$Id: file.h,v 1.24 2014/05/18 17:50:11 espie Exp $
*/
#ifndef __file_h__
@@ -340,6 +340,7 @@ protected void file_delmagic(struct magic *, int type, size_t entries);
protected void file_badread(struct magic_set *);
protected void file_badseek(struct magic_set *);
protected void file_oomem(struct magic_set *, size_t);
+protected void file_oomem2(struct magic_set *, size_t, size_t);
protected void file_error(struct magic_set *, int, const char *, ...);
protected void file_magerror(struct magic_set *, const char *, ...);
protected void file_magwarn(struct magic_set *, const char *, ...);
diff --git a/usr.bin/file/funcs.c b/usr.bin/file/funcs.c
index 754b110ed71..cdc593ee54f 100644
--- a/usr.bin/file/funcs.c
+++ b/usr.bin/file/funcs.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: funcs.c,v 1.7 2009/10/27 23:59:37 deraadt Exp $ */
+/* $OpenBSD: funcs.c,v 1.8 2014/05/18 17:50:11 espie Exp $ */
/*
* Copyright (c) Christos Zoulas 2003.
* All Rights Reserved.
@@ -122,6 +122,11 @@ file_oomem(struct magic_set *ms, size_t len)
}
protected void
+file_oomem2(struct magic_set *ms, size_t len, size_t l2)
+{
+ file_error(ms, errno, "cannot allocate %zu * %zu bytes", len, l2);
+}
+protected void
file_badseek(struct magic_set *ms)
{
file_error(ms, errno, "error seeking");