From 1524a4e6a6ddb24d04415934f5726a105268d5a6 Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Mon, 11 Dec 2006 21:48:49 +0000 Subject: [PATCH] - fix byteswapping issue - report the number of bytes we tried to allocate when allocation fails - add a few missed cases in the strength routine --- src/apprentice.c | 44 ++++++++++++++++++++++++-------------------- src/file.h | 4 ++-- src/funcs.c | 10 +++++----- src/softmagic.c | 6 +++--- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/apprentice.c b/src/apprentice.c index 0ca23263..974fae95 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -46,7 +46,7 @@ #endif #ifndef lint -FILE_RCSID("@(#)$Id: apprentice.c,v 1.99 2006/12/08 20:31:07 christos Exp $") +FILE_RCSID("@(#)$Id: apprentice.c,v 1.100 2006/12/11 21:48:49 christos Exp $") #endif /* lint */ #define EATAB {while (isascii((unsigned char) *l) && \ @@ -203,7 +203,7 @@ apprentice_1(struct magic_set *ms, const char *fn, int action, if ((ml = malloc(sizeof(*ml))) == NULL) { file_delmagic(magic, mapped, nmagic); - file_oomem(ms); + file_oomem(ms, sizeof(*ml)); return -1; } @@ -257,13 +257,13 @@ file_apprentice(struct magic_set *ms, const char *fn, int action) fn = MAGIC; if ((fn = mfn = strdup(fn)) == NULL) { - file_oomem(ms); + file_oomem(ms, strlen(fn)); return NULL; } if ((mlist = malloc(sizeof(*mlist))) == NULL) { free(mfn); - file_oomem(ms); + file_oomem(ms, sizeof(*mlist)); return NULL; } mlist->next = mlist->prev = mlist; @@ -275,10 +275,11 @@ file_apprentice(struct magic_set *ms, const char *fn, int action) if (*fn == '\0') break; if (ms->flags & MAGIC_MIME) { - if ((afn = malloc(strlen(fn) + sizeof(mime))) == NULL) { + size_t len = strlen(fn) + sizeof(mime); + if ((afn = malloc(len)) == NULL) { free(mfn); free(mlist); - file_oomem(ms); + file_oomem(ms, len); return NULL; } (void)strcpy(afn, fn); @@ -358,6 +359,9 @@ apprentice_magic_strength(const struct magic *m) val += 4 * MULT; break; + case FILE_QUAD: + case FILE_BEQUAD: + case FILE_LEQUAD: case FILE_QDATE: case FILE_LEQDATE: case FILE_BEQDATE: @@ -448,7 +452,7 @@ apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, maxmagic = MAXMAGIS; if ((marray = calloc(maxmagic, sizeof(*marray))) == NULL) { (void)fclose(f); - file_oomem(ms); + file_oomem(ms, maxmagic * sizeof(*marray)); return -1; } marraycount = 0; @@ -487,7 +491,7 @@ apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, mentrycount += marray[i].cont_count; if ((*magicp = malloc(sizeof(**magicp) * mentrycount)) == NULL) { - file_oomem(ms); + file_oomem(ms, sizeof(**magicp) * mentrycount); errs++; goto out; } @@ -611,7 +615,7 @@ parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp, struct magic *nm; size_t cnt = me->max_count + ALLOC_CHUNK; if ((nm = realloc(me->mp, sizeof(*nm) * cnt)) == NULL) { - file_oomem(ms); + file_oomem(ms, sizeof(*nm) * cnt); return -1; } me->mp = m = nm; @@ -627,7 +631,7 @@ parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp, maxmagic += ALLOC_INCR; if ((mp = realloc(*mentryp, sizeof(*mp) * maxmagic)) == NULL) { - file_oomem(ms); + file_oomem(ms, sizeof(*mp) * maxmagic); return -1; } (void)memset(&mp[*nmentryp], 0, sizeof(*mp) * @@ -637,7 +641,7 @@ 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); + file_oomem(ms, sizeof(*m) * ALLOC_CHUNK); return -1; } me->mp = m; @@ -1350,7 +1354,7 @@ apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, #define RET 2 #else if ((mm = malloc((size_t)st.st_size)) == NULL) { - file_oomem(ms); + file_oomem(ms, (size_t)st.st_size); goto error; } if (read(fd, mm, (size_t)st.st_size) != (size_t)st.st_size) { @@ -1512,14 +1516,14 @@ swap8(uint64_t sv) uint32_t rv; uint8_t *s = (uint8_t *)(void *)&sv; uint8_t *d = (uint8_t *)(void *)&rv; - d[0] = s[7]; - d[1] = s[6]; - d[2] = s[5]; - d[3] = s[4]; - d[4] = s[3]; - d[5] = s[2]; - d[6] = s[1]; - d[7] = s[0]; + d[0] = s[3]; + d[1] = s[2]; + d[2] = s[1]; + d[3] = s[0]; + d[4] = s[7]; + d[5] = s[6]; + d[6] = s[5]; + d[7] = s[4]; return rv; } diff --git a/src/file.h b/src/file.h index d223d5c2..bb2fc166 100644 --- a/src/file.h +++ b/src/file.h @@ -27,7 +27,7 @@ */ /* * file.h - definitions for file(1) program - * @(#)$Id: file.h,v 1.82 2006/12/08 20:31:07 christos Exp $ + * @(#)$Id: file.h,v 1.83 2006/12/11 21:48:49 christos Exp $ */ #ifndef __file_h__ @@ -307,7 +307,7 @@ protected uint64_t file_signextend(struct magic_set *, struct magic *, uint64_t) 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 *); +protected void file_oomem(struct magic_set *, size_t); protected void file_error(struct magic_set *, int, const char *, ...); protected void file_magwarn(struct magic_set *, const char *, ...); protected void file_mdump(struct magic *); diff --git a/src/funcs.c b/src/funcs.c index ea0a54c6..824e3a09 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -38,7 +38,7 @@ #endif #ifndef lint -FILE_RCSID("@(#)$Id: funcs.c,v 1.22 2006/10/31 19:37:17 christos Exp $") +FILE_RCSID("@(#)$Id: funcs.c,v 1.23 2006/12/11 21:48:49 christos Exp $") #endif /* lint */ #ifndef HAVE_VSNPRINTF @@ -60,7 +60,7 @@ file_printf(struct magic_set *ms, const char *fmt, ...) if ((len = vsnprintf(ms->o.ptr, ms->o.len, fmt, ap)) >= ms->o.len) { va_end(ap); if ((buf = realloc(ms->o.buf, len + 1024)) == NULL) { - file_oomem(ms); + file_oomem(ms, len + 1024); return -1; } ms->o.ptr = buf + (ms->o.ptr - ms->o.buf); @@ -102,9 +102,9 @@ file_error(struct magic_set *ms, int error, const char *f, ...) protected void -file_oomem(struct magic_set *ms) +file_oomem(struct magic_set *ms, size_t len) { - file_error(ms, errno, "cannot allocate memory"); + file_error(ms, errno, "cannot allocate %zu bytes", len); } protected void @@ -184,7 +184,7 @@ file_getbuffer(struct magic_set *ms) nsize = ms->o.len * 4 + 1; if (ms->o.psize < nsize) { if ((nbuf = realloc(ms->o.pbuf, nsize)) == NULL) { - file_oomem(ms); + file_oomem(ms, nsize); return NULL; } ms->o.psize = nsize; diff --git a/src/softmagic.c b/src/softmagic.c index 5d96261a..77250487 100644 --- a/src/softmagic.c +++ b/src/softmagic.c @@ -39,7 +39,7 @@ #ifndef lint -FILE_RCSID("@(#)$Id: softmagic.c,v 1.86 2006/12/08 20:31:07 christos Exp $") +FILE_RCSID("@(#)$Id: softmagic.c,v 1.87 2006/12/11 21:48:49 christos Exp $") #endif /* lint */ private int match(struct magic_set *, struct magic *, uint32_t, @@ -257,7 +257,7 @@ check_mem(struct magic_set *ms, unsigned int level) ms->c.off = (ms->c.off == NULL) ? malloc(len) : realloc(ms->c.off, len); if (ms->c.off != NULL) return 0; - file_oomem(ms); + file_oomem(ms, len); return -1; } @@ -630,7 +630,7 @@ mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir, return 0; } if ((p->search.buf = strdup((const char *)s)) == NULL) { - file_oomem(ms); + file_oomem(ms, strlen((const char *)s)); return -1; } for (b = p->search.buf; offset && -- 2.40.0