]> granicus.if.org Git - file/commitdiff
- Treat filenames more sanely.
authorChristos Zoulas <christos@zoulas.com>
Wed, 8 Oct 2003 16:37:27 +0000 (16:37 +0000)
committerChristos Zoulas <christos@zoulas.com>
Wed, 8 Oct 2003 16:37:27 +0000 (16:37 +0000)
- Handle freeing of magic in one place.
- Fix memory leak.

src/apprentice.c
src/file.c
src/file.h
src/magic.c

index 2353dd002ed4e32e9b8f8b5993596176803b6b73..2af4a3277aebd09db64ceeafe84cf947484d8c49 100644 (file)
@@ -50,7 +50,7 @@
 #endif
 
 #ifndef        lint
-FILE_RCSID("@(#)$Id: apprentice.c,v 1.64 2003/09/12 19:39:44 christos Exp $")
+FILE_RCSID("@(#)$Id: apprentice.c,v 1.65 2003/10/08 16:37:27 christos Exp $")
 #endif /* lint */
 
 #define        EATAB {while (isascii((unsigned char) *l) && \
@@ -75,13 +75,6 @@ FILE_RCSID("@(#)$Id: apprentice.c,v 1.64 2003/09/12 19:39:44 christos Exp $")
 #define MAP_FILE 0
 #endif
 
-#ifdef __EMX__
-  char PATHSEP=';';
-#else
-  char PATHSEP=':';
-#endif
-
-
 private int getvalue(struct magic_set *ms, struct magic *, char **);
 private int hextoint(int);
 private char *getstr(struct magic_set *, char *, char *, int, int *);
@@ -172,12 +165,15 @@ apprentice_1(struct magic_set *ms, const char *fn, int action,
        mapped = rv;
             
        if ((ml = malloc(sizeof(*ml))) == NULL) {
+               file_delmagic(magic, mapped, nmagic);
                file_oomem(ms);
                return -1;
        }
 
-       if (magic == NULL || nmagic == 0)
+       if (magic == NULL || nmagic == 0) {
+               file_delmagic(magic, mapped, nmagic);
                return -1;
+       }
 
        ml->magic = magic;
        ml->nmagic = nmagic;
@@ -192,19 +188,45 @@ apprentice_1(struct magic_set *ms, const char *fn, int action,
 #endif /* COMPILE_ONLY */
 }
 
+protected void
+file_delmagic(struct magic *p, int type, size_t entries)
+{
+       if (p == NULL)
+               return;
+       switch (type) {
+       case 2:
+               p--;
+               (void)munmap(p, sizeof(*p) * (entries + 1));
+               break;
+       case 1:
+               p--;
+       case 0:
+               free(p);
+               break;
+       default:
+               abort();
+       }
+}
+
 
 /* const char *fn: list of magic files */
 protected struct mlist *
 file_apprentice(struct magic_set *ms, const char *fn, int action)
 {
-       char *p, *mfn;
+       char *p, *mfn, *afn = NULL;
        int file_err, errs = -1;
        struct mlist *mlist;
 
+       if (fn == NULL)
+               fn = getenv("MAGIC");
+       if (fn == NULL)
+               fn = MAGIC;
+
        if ((fn = mfn = strdup(fn)) == NULL) {
                file_oomem(ms);
                return NULL;
        }
+
        if ((mlist = malloc(sizeof(*mlist))) == NULL) {
                free(mfn);
                file_oomem(ms);
@@ -216,9 +238,26 @@ file_apprentice(struct magic_set *ms, const char *fn, int action)
                p = strchr(fn, PATHSEP);
                if (p)
                        *p++ = '\0';
+               if (*fn == '\0')
+                       break;
+               if (ms->flags & MAGIC_MIME) {
+                       if ((afn = malloc(strlen(fn) + 5 + 1)) == NULL) {
+                               free(mfn);
+                               free(mlist);
+                               file_oomem(ms);
+                               return NULL;
+                       }
+                       (void)strcpy(afn, fn);
+                       (void)strcat(afn, ".mime");
+                       fn = afn;
+               }
                file_err = apprentice_1(ms, fn, action, mlist);
                if (file_err > errs)
                        errs = file_err;
+               if (afn) {
+                       free(afn);
+                       afn = NULL;
+               }
                fn = p;
        }
        if (errs == -1) {
@@ -228,7 +267,6 @@ file_apprentice(struct magic_set *ms, const char *fn, int action)
                file_error(ms, "Couldn't find any magic files!");
                return NULL;
        }
-       free(mfn);
        return mlist;
 }
 
index 6cc49d539550b2d0f7dc662e2a69dbe25346c3c6..5a69f1654fefd2b77bb92b51cd7f65e2bb5c42c8 100644 (file)
@@ -72,7 +72,7 @@
 #include "patchlevel.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$Id: file.c,v 1.81 2003/09/12 19:39:44 christos Exp $")
+FILE_RCSID("@(#)$Id: file.c,v 1.82 2003/10/08 16:37:27 christos Exp $")
 #endif /* lint */
 
 
@@ -84,10 +84,6 @@ FILE_RCSID("@(#)$Id: file.c,v 1.81 2003/09/12 19:39:44 christos Exp $")
 
 # define USAGE  "Usage: %s [-bcik" SYMLINKFLAG "nNsvz] [-f namefile] [-F separator] [-m magicfiles] file...\n       %s -C -m magicfiles\n"
 
-#ifndef MAGIC
-# define MAGIC "/etc/magic"
-#endif
-
 #ifndef MAXPATHLEN
 #define        MAXPATHLEN      512
 #endif
@@ -96,8 +92,7 @@ private int           /* Global command-line options          */
        bflag = 0,      /* brief output format                  */
        nopad = 0,      /* Don't pad output                     */
        nobuffer = 0,   /* Do not buffer stdout                 */
-       kflag = 0,      /* Keep going after the first match     */
-       rflag = 0;      /* Restore access times of files        */
+       kflag = 0;      /* Keep going after the first match     */
 
 private const char *magicfile = 0;     /* where the magic is   */
 private const char *default_magicfile = MAGIC;
@@ -229,11 +224,6 @@ main(int argc, char *argv[])
                        break;
                case 'i':
                        flags |= MAGIC_MIME;
-                       if ((mime = malloc(strlen(magicfile) + 6)) != NULL) {
-                               (void)strcpy(mime, magicfile);
-                               (void)strcat(mime, ".mime");
-                               magicfile = mime;
-                       }
                        break;
                case 'k':
                        kflag = 1;
index f82241f839ab3be2d13a9bba5f7d453429346903..c05a4fe28d5e25c9606c482a0cdb9588741b29fe 100644 (file)
@@ -32,7 +32,7 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$Id: file.h,v 1.56 2003/09/12 19:07:25 christos Exp $
+ * @(#)$Id: file.h,v 1.57 2003/10/08 16:37:27 christos Exp $
  */
 
 #ifndef __file_h__
 /* Do this here and now, because struct stat gets re-defined on solaris */
 #include <sys/stat.h>
 
+#ifndef MAGIC
+#define MAGIC "/etc/magic"
+#endif
+
+#ifdef __EMX__
+#define PATHSEP        ';'
+#else
+#define PATHSEP        ':'
+#endif
 
 #define private static
 #ifndef protected
@@ -71,6 +80,7 @@
 #define VERSIONNO      2
 #define FILE_MAGICSIZE (32 * 4)
 
+#define        FILE_LOAD       0
 #define FILE_CHECK     1
 #define FILE_COMPILE   2
 
@@ -191,6 +201,7 @@ protected int file_is_tar(struct magic_set *, const unsigned char *, size_t);
 protected int file_softmagic(struct magic_set *, const unsigned char *, size_t);
 protected struct mlist *file_apprentice(struct magic_set *, const char *, int);
 protected uint32_t file_signextend(struct magic_set *, struct magic *, uint32_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 *);
index e3a454c6dbf3917bdbe80ea0b361b476422b1579..a0eccb4cdd3f8434872a1db4022bc17b98e567c9 100644 (file)
@@ -65,7 +65,7 @@
 #include "patchlevel.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$Id: magic.c,v 1.10 2003/07/10 21:07:14 christos Exp $")
+FILE_RCSID("@(#)$Id: magic.c,v 1.11 2003/10/08 16:37:27 christos Exp $")
 #endif /* lint */
 
 #ifdef __EMX__
@@ -74,10 +74,6 @@ protected int file_os2_apptype(struct magic_set *ms, const char *fn,
     const void *buf, size_t nb);
 #endif /* __EMX__ */
 
-#ifndef MAGIC
-# define MAGIC "/etc/magic"
-#endif
-
 private void free_mlist(struct mlist *);
 private void close_and_restore(const struct magic_set *, const char *, int,
     const struct stat *);
@@ -113,25 +109,6 @@ magic_open(int flags)
        return ms;
 }
 
-/*
- * load a magic file
- */
-public int
-magic_load(struct magic_set *ms, const char *magicfile)
-{
-       struct mlist *ml;
-
-       if (magicfile == NULL)
-               magicfile = (ms->flags & MAGIC_MIME) ? MAGIC ".mime" : MAGIC;
-
-       ml = file_apprentice(ms, magicfile, 0);
-       if (ml == NULL) 
-               return -1;
-       free_mlist(ms->mlist);
-       ms->mlist = ml;
-       return 0;
-}
-
 private void
 free_mlist(struct mlist *mlist)
 {
@@ -143,19 +120,7 @@ free_mlist(struct mlist *mlist)
        for (ml = mlist->next; ml != mlist;) {
                struct mlist *next = ml->next;
                struct magic *mg = ml->magic;
-               switch (ml->mapped) {
-               case 0:
-                       free(mg);
-                       break;
-               case 1:
-                       mg--;
-                       free(mg);
-                       break;
-               case 2:
-                       mg--;
-                       (void)munmap(mg, sizeof(*mg) * (ml->nmagic + 1));
-                       break;
-               }
+               file_delmagic(mg, ml->mapped, ml->nmagic);
                free(ml);
                ml = next;
        }
@@ -172,12 +137,25 @@ magic_close(ms)
        free(ms);
 }
 
+/*
+ * load a magic file
+ */
+public int
+magic_load(struct magic_set *ms, const char *magicfile)
+{
+       struct mlist *ml = file_apprentice(ms, magicfile, FILE_LOAD);
+       if (ml) {
+               free_mlist(ms->mlist);
+               ms->mlist = ml;
+               return 0;
+       }
+       return -1;
+}
+
 public int
 magic_compile(struct magic_set *ms, const char *magicfile)
 {
        struct mlist *ml = file_apprentice(ms, magicfile, FILE_COMPILE);
-       if(ml == NULL)
-               return -1;
        free_mlist(ml);
        return ml ? 0 : -1;
 }
@@ -186,8 +164,6 @@ public int
 magic_check(struct magic_set *ms, const char *magicfile)
 {
        struct mlist *ml = file_apprentice(ms, magicfile, FILE_CHECK);
-       if(ml == NULL)
-               return -1;
        free_mlist(ml);
        return ml ? 0 : -1;
 }