From f00c339cd0b8c7138b985a5d97301a42d0974a0e Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Fri, 9 May 2008 14:20:28 +0000 Subject: [PATCH] - compile with all warnings turned on. - don't tack .mgc if the file ends in .mgc --- src/Makefile.am | 4 ++++ src/apprentice.c | 62 ++++++++++++++++++++++++++---------------------- src/ascmagic.c | 3 +-- src/file.h | 15 ++++++++---- src/fsmagic.c | 4 ++-- src/funcs.c | 3 +-- src/vasprintf.c | 6 ++--- 7 files changed, 55 insertions(+), 42 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 99934dab..b5694930 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,6 +5,10 @@ include_HEADERS = magic.h bin_PROGRAMS = file AM_CPPFLAGS = -DMAGIC='"$(MAGIC)"' +AM_CFLAGS = -Wall -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith \ + -Wmissing-declarations -Wredundant-decls -Wnested-externs \ + -Wsign-compare -Wreturn-type -Wswitch -Wshadow \ + -Wcast-qual -Wwrite-strings -Wextra -Wunused-parameter libmagic_la_SOURCES = magic.c apprentice.c softmagic.c ascmagic.c \ compress.c is_tar.c readelf.c print.c fsmagic.c \ diff --git a/src/apprentice.c b/src/apprentice.c index 987dc93c..63f5e0ce 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -49,7 +49,7 @@ #include #ifndef lint -FILE_RCSID("@(#)$File: apprentice.c,v 1.133 2008/04/04 15:01:09 christos Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.134 2008/04/29 01:11:39 christos Exp $") #endif /* lint */ #define EATAB {while (isascii((unsigned char) *l) && \ @@ -505,7 +505,7 @@ apprentice_sort(const void *a, const void *b) return 1; } -private int +private void set_test_type(struct magic *mstart, struct magic *m) { switch (m->type) { @@ -550,7 +550,7 @@ set_test_type(struct magic *mstart, struct magic *m) case FILE_REGEX: case FILE_SEARCH: /* binary test if pattern is not text */ - if (file_looks_utf8(m->value.s, m->vallen, NULL, NULL) <= 0) + if (file_looks_utf8(m->value.us, m->vallen, NULL, NULL) <= 0) mstart->flag |= BINTEST; break; case FILE_DEFAULT: @@ -643,10 +643,11 @@ apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode)) { dir = opendir(fn); if (dir) { - while (d = readdir(dir)) { + while ((d = readdir(dir)) != NULL) { snprintf(subfn, sizeof(subfn), "%s/%s", fn, d->d_name); - if (stat(subfn, &st) == 0 && S_ISREG(st.st_mode)) { + if (stat(subfn, &st) == 0 && + S_ISREG(st.st_mode)) { load_1(ms, action, subfn, &errs, &marray, &marraycount); } @@ -668,26 +669,27 @@ apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, starttest = i; do { + static const char text[] = "text"; + static const char binary[] = "binary"; + static const size_t len = sizeof(text); set_test_type(marray[starttest].mp, marray[i].mp); - if (ms->flags & MAGIC_DEBUG) { - (void)fprintf(stderr, "%s%s%s: %s\n", - marray[i].mp->mimetype, - marray[i].mp->mimetype[0] == '\0' ? "" : "; ", - marray[i].mp->desc[0] ? marray[i].mp->desc : "(no description)", - marray[i].mp->flag & BINTEST ? "binary" : "text"); - if (marray[i].mp->flag & BINTEST) { -#define SYMBOL "text" -#define SYMLEN sizeof(SYMBOL) - char *p = strstr(marray[i].mp->desc, "text"); - if (p && (p == marray[i].mp->desc || isspace(p[-1])) && - (p + SYMLEN - marray[i].mp->desc == MAXstring || - (p[SYMLEN] == '\0' || isspace(p[SYMLEN])))) { - (void)fprintf(stderr, - "*** Possible binary test for text type\n"); - } -#undef SYMBOL -#undef SYMLEN - } + if ((ms->flags & MAGIC_DEBUG) == 0) + continue; + (void)fprintf(stderr, "%s%s%s: %s\n", + marray[i].mp->mimetype, + marray[i].mp->mimetype[0] == '\0' ? "" : "; ", + marray[i].mp->desc[0] ? marray[i].mp->desc : + "(no description)", + marray[i].mp->flag & BINTEST ? binary : text); + if (marray[i].mp->flag & BINTEST) { + char *p = strstr(marray[i].mp->desc, text); + if (p && (p == marray[i].mp->desc || + isspace((unsigned char)p[-1])) && + (p + len - marray[i].mp->desc == + MAXstring || (p[len] == '\0' || + isspace((unsigned char)p[len])))) + (void)fprintf(stderr, "*** Possible " + "binary test for text type\n"); } } while (++i < marraycount && marray[i].mp->cont_level != 0); } @@ -1938,7 +1940,7 @@ apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, ptr = (uint32_t *)(void *)*magicp; if (*ptr != MAGICNO) { if (swap4(*ptr) != MAGICNO) { - file_error(ms, 0, "bad magic in `%s'"); + file_error(ms, 0, "bad magic in `%s'", dbname); goto error1; } needsbyteswap = 1; @@ -2036,14 +2038,18 @@ private const char ext[] = ".mgc"; private void mkdbname(const char *fn, char **buf, int strip) { + const char *p; if (strip) { - const char *p; if ((p = strrchr(fn, '/')) != NULL) fn = ++p; } - (void)asprintf(buf, "%s%s", fn, ext); - if (*buf && strlen(*buf) > MAXPATHLEN) { + if ((p = strstr(fn, ext)) != NULL && p[sizeof(ext) - 1] == '\0') + *buf = strdup(fn); + else + (void)asprintf(buf, "%s%s", fn, ext); + + if (buf && *buf && strlen(*buf) > MAXPATHLEN) { free(*buf); *buf = NULL; } diff --git a/src/ascmagic.c b/src/ascmagic.c index 4576fcad..75ed43bb 100644 --- a/src/ascmagic.c +++ b/src/ascmagic.c @@ -49,7 +49,7 @@ #include "names.h" #ifndef lint -FILE_RCSID("@(#)$File: ascmagic.c,v 1.61 2008/02/27 15:02:33 rrt Exp $") +FILE_RCSID("@(#)$File: ascmagic.c,v 1.62 2008/03/01 22:21:48 rrt Exp $") #endif /* lint */ #define MAXLINELEN 300 /* longest sane line length */ @@ -59,7 +59,6 @@ FILE_RCSID("@(#)$File: ascmagic.c,v 1.61 2008/02/27 15:02:33 rrt Exp $") private int looks_ascii(const unsigned char *, size_t, unichar *, size_t *); private int looks_utf8_with_BOM(const unsigned char *, size_t, unichar *, size_t *); -protected int file_looks_utf8(const unsigned char *, size_t, unichar *, size_t *); private int looks_ucs16(const unsigned char *, size_t, unichar *, size_t *); private int looks_latin1(const unsigned char *, size_t, unichar *, size_t *); private int looks_extended(const unsigned char *, size_t, unichar *, size_t *); diff --git a/src/file.h b/src/file.h index f00693f3..b8b211b8 100644 --- a/src/file.h +++ b/src/file.h @@ -27,7 +27,7 @@ */ /* * file.h - definitions for file(1) program - * @(#)$File: file.h,v 1.102 2008/02/24 01:35:58 christos Exp $ + * @(#)$File: file.h,v 1.103 2008/03/01 22:21:49 rrt Exp $ */ #ifndef __file_h__ @@ -243,6 +243,7 @@ struct magic { uint8_t hl[4]; /* 4 bytes of a fixed-endian "long" */ uint8_t hq[8]; /* 8 bytes of a fixed-endian "quad" */ char s[MAXstring]; /* the search string or regex pattern */ + unsigned char us[MAXstring]; float f; double d; } value; /* either number or string */ @@ -323,7 +324,8 @@ protected int file_buffer(struct magic_set *, int, const char *, const void *, size_t); protected int file_fsmagic(struct magic_set *, const char *, struct stat *); protected int file_pipe2file(struct magic_set *, int, const void *, size_t); -protected int file_printf(struct magic_set *, const char *, ...); +protected int file_printf(struct magic_set *, const char *, ...) + __attribute__((__format__(__printf__, 2, 3))); protected int file_reset(struct magic_set *); protected int file_tryelf(struct magic_set *, int, const unsigned char *, size_t); @@ -339,9 +341,12 @@ 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_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 *, ...); +protected void file_error(struct magic_set *, int, const char *, ...) + __attribute__((__format__(__printf__, 3, 4))); +protected void file_magerror(struct magic_set *, const char *, ...) + __attribute__((__format__(__printf__, 2, 3))); +protected void file_magwarn(struct magic_set *, const char *, ...) + __attribute__((__format__(__printf__, 2, 3))); protected void file_mdump(struct magic *); protected void file_showstr(FILE *, const char *, size_t); protected size_t file_mbswidth(const char *); diff --git a/src/fsmagic.c b/src/fsmagic.c index d4353285..4bb62dc0 100644 --- a/src/fsmagic.c +++ b/src/fsmagic.c @@ -57,13 +57,13 @@ #undef HAVE_MAJOR #ifndef lint -FILE_RCSID("@(#)$File: fsmagic.c,v 1.49 2008/02/07 00:58:52 christos Exp $") +FILE_RCSID("@(#)$File: fsmagic.c,v 1.50 2008/02/12 17:22:54 rrt Exp $") #endif /* lint */ private int bad_link(struct magic_set *ms, int err, char *buf) { - char *errfmt; + const char *errfmt; if (err == ELOOP) errfmt = "symbolic link in a loop"; else diff --git a/src/funcs.c b/src/funcs.c index e804c3be..ef3d3e7d 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -38,7 +38,7 @@ #endif #ifndef lint -FILE_RCSID("@(#)$File: funcs.c,v 1.38 2008/02/19 00:58:59 rrt Exp $") +FILE_RCSID("@(#)$File: funcs.c,v 1.39 2008/03/01 22:21:49 rrt Exp $") #endif /* lint */ /* @@ -48,7 +48,6 @@ protected int file_printf(struct magic_set *ms, const char *fmt, ...) { va_list ap; - size_t size; int len; char *buf, *newstr; diff --git a/src/vasprintf.c b/src/vasprintf.c index 6e1846b7..2ee73121 100644 --- a/src/vasprintf.c +++ b/src/vasprintf.c @@ -265,7 +265,7 @@ static int getint(const char **string) { int i = 0; - while (isdigit(**string) != 0) { + while (isdigit((unsigned char)**string) != 0) { i = i * 10 + (**string - '0'); (*string)++; } @@ -356,7 +356,7 @@ static int dispatch(xprintf_struct *s) width = va_arg(s->vargs, int); if ((size_t)width > 0x3fffU) /* 'size_t' to check against negative values too */ width = 0x3fff; - } else if (isdigit(*SRCTXT)) /* width given as ASCII number */ + } else if (isdigit((unsigned char)*SRCTXT)) /* width given as ASCII number */ width = getint(&SRCTXT); else width = -1; /* no width specified */ @@ -372,7 +372,7 @@ static int dispatch(xprintf_struct *s) if ((size_t)prec >= 0x3fffU) /* 'size_t' to check against negative values too */ prec = 0x3fff; } else { /* .prec given as ASCII number */ - if (isdigit(*SRCTXT) == 0) + if (isdigit((unsigned char)*SRCTXT) == 0) INCOHERENT(); prec = getint(&SRCTXT); } -- 2.40.0