Add mcheck functionality throughout.
authorjbj <devnull@localhost>
Wed, 2 Oct 2002 16:45:37 +0000 (16:45 +0000)
committerjbj <devnull@localhost>
Wed, 2 Oct 2002 16:45:37 +0000 (16:45 +0000)
CVS patchset: 5747
CVS date: 2002/10/02 16:45:37

file/Makefile.am
file/apprentice.c
file/compress.c
file/configure.ac
file/file.c
file/softmagic.c
file/system.h

index 02482e6..99a9764 100644 (file)
@@ -20,7 +20,7 @@ man_MANS = file.1 $(man_MAGIC)
 file_SOURCES = file.c apprentice.c fsmagic.c softmagic.c ascmagic.c \
        compress.c is_tar.c readelf.c print.c \
        file.h names.h patchlevel.h readelf.h tar.h
-file_LDFLAGS = -all-static
+#file_LDFLAGS = -all-static
 
 EXTRA_DIST = LEGAL.NOTICE MAINT Makefile.std magic2mime \
        Localstuff Header $(magic_FRAGMENTS) file.man magic.man
index ca3938a..b525833 100644 (file)
@@ -299,25 +299,12 @@ parse(/*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp,
        if (*nmagicp + 1 >= maxmagic){
                maxmagic += ALLOC_INCR;
 /*@-unqualifiedtrans @*/
-               if ((m = (struct magic *) realloc(*magicp,
-                   sizeof(struct magic) * maxmagic)) == NULL) {
-                       (void) fprintf(stderr, "%s: Out of memory (%s).\n",
-                           progname, strerror(errno));
-/*@-usereleased@*/
-                       if (*magicp != NULL)
-                               free(*magicp);
-/*@=usereleased@*/
-                       if (action == CHECK)
-                               return -1;
-                       else
-                               exit(EXIT_FAILURE);
-               }
+               *magicp = xrealloc(*magicp, sizeof(struct magic) * maxmagic);
 /*@=unqualifiedtrans @*/
-               *magicp = m;
-               memset(&(*magicp)[*nmagicp], 0, sizeof(struct magic)
-                   * ALLOC_INCR);
-       }
-       m = &(*magicp)[*nmagicp];
+               m = &(*magicp)[*nmagicp];
+               memset(m, 0, sizeof(struct magic) * ALLOC_INCR);
+       } else
+               m = &(*magicp)[*nmagicp];
        m->flag = 0;
        m->cont_level = 0;
 
@@ -774,7 +761,6 @@ byteswap(struct magic *m, uint32_t nmagic)
 /*
  * make a dbname
  */
-/*@null@*/
 static char *
 mkdbname(const char *fn)
        /*@globals fileSystem @*/
@@ -783,16 +769,8 @@ mkdbname(const char *fn)
        static const char ext[] = ".mgc";
        /*@only@*/
        static char *buf = NULL;
-       size_t len = strlen(fn) + sizeof(ext) + 1;
-       if (buf == NULL)
-               buf = malloc(len);
-       else
-               buf = realloc(buf, len);
-       if (buf == NULL) {
-               (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname,
-                   strerror(errno));
-               return NULL;
-       }
+
+       buf = xrealloc(buf, strlen(fn) + sizeof(ext) + 1);
        (void)strcpy(buf, fn);
        (void)strcat(buf, ext);
        return buf;
@@ -825,13 +803,7 @@ apprentice_file(/*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp,
        }
 
         maxmagic = MAXMAGIS;
-       *magicp = (struct magic *) calloc(sizeof(struct magic), maxmagic);
-       if (*magicp == NULL) {
-               (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname,
-                   strerror(errno));
-               if (action == CHECK)
-                       return -1;
-       }
+       *magicp = (struct magic *) xcalloc(sizeof(struct magic), maxmagic);
 
        /* parse it */
        if (action == CHECK)    /* print silly verbose header for USG compat. */
@@ -867,6 +839,7 @@ apprentice_compile(/*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp,
 {
        int fd;
        char *dbname = mkdbname(fn);
+       /*@observer@*/
        static const uint32_t ar[] = {
            MAGICNO, VERSIONNO
        };
@@ -940,11 +913,7 @@ apprentice_map(/*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp,
                goto error;
        }
 #else
-       if ((mm = malloc((size_t)st.st_size)) == NULL) {
-               (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname,
-                    strerror(errno));
-               goto error;
-       }
+       mm = xmalloc((size_t)st.st_size);
        if (read(fd, mm, (size_t)st.st_size) != (size_t)st.st_size) {
                (void) fprintf(stderr, "%s: Read failed (%s).\n", progname,
                    strerror(errno));
@@ -1031,12 +1000,7 @@ apprentice_1(const char *fn, int action)
        if (rv != 0)
                return rv;
             
-       if ((ml = malloc(sizeof(*ml))) == NULL) {
-               (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname,
-                   strerror(errno));
-               if (action == CHECK)
-                       return -1;
-       }
+       ml = xmalloc(sizeof(*ml));
 
        if (magic == NULL || nmagic == 0)
                return rv;
@@ -1070,17 +1034,8 @@ apprentice(const char *fn, int action)
        mlist.next = &mlist;
        mlist.prev = &mlist;
 /*@=immediatetrans@*/
-       mfn = malloc(strlen(fn)+1);
-       if (mfn == NULL) {
-               (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname,
-                   strerror(errno));
-/*@-compmempass@*/
-               if (action == CHECK)
-                       return -1;
-               else
-                       exit(EXIT_FAILURE);
-/*@=compmempass@*/
-       }
+
+       mfn = xmalloc(strlen(fn)+1);
        fn = strcpy(mfn, fn);
   
 /*@-branchstate@*/
index 6dac3d8..f45e893 100644 (file)
@@ -183,9 +183,7 @@ uncompressgzipped(const unsigned char *old,
        if(flg & FHCRC)
                data_start += 2;
 
-       if ((*newch = (unsigned char *)malloc(HOWMANY + 1)) == NULL) {
-               return 0;
-       }
+       *newch = (unsigned char *) xmalloc(HOWMANY + 1);
        
        z.next_in = (Bytef *)(old + data_start);
        z.avail_in = n - data_start;
@@ -274,10 +272,7 @@ uncompressbuf(int method, const unsigned char *old,
                }
                (void) close(fdin[1]);
                fdin[1] = -1;
-               if ((*newch = (unsigned char *) malloc(HOWMANY + 1)) == NULL) {
-                       n = 0;
-                       goto err;
-               }
+               *newch = (unsigned char *) xmalloc(HOWMANY + 1);
                if ((n = sread(fdout[0], *newch, HOWMANY)) <= 0) {
                        free(*newch);
                        n = 0;
index 08539dc..9ecc67c 100644 (file)
@@ -59,13 +59,14 @@ AC_PROG_LIBTOOL
 
 dnl Checks for headers
 AC_HEADER_STDC
+AC_HEADER_STDINT
 AC_HEADER_MAJOR
 AC_HEADER_SYS_WAIT
-AC_HEADER_STDINT
 AC_CHECK_HEADERS(sys/mman.h sys/stat.h sys/types.h)
 AC_CHECK_HEADERS(fcntl.h)
 AC_CHECK_HEADERS(getopt.h)
 AC_CHECK_HEADERS(locale.h)
+AC_CHECK_HEADERS(mcheck.h)
 AC_CHECK_HEADERS(regex.h)
 AC_CHECK_HEADERS(unistd.h)
 
@@ -95,9 +96,10 @@ AC_CHECK_SIZEOF_STDC_HEADERS(uint32_t, 0)
 AC_CHECK_SIZEOF_STDC_HEADERS(uint64_t, 0)
 
 dnl Checks for functions
-AC_CHECK_FUNCS(mmap strerror strtoul mkstemp)
+AC_CHECK_FUNCS(mtrace mkstemp mmap strdup strerror strtoul)
 
 dnl Checks for libraries
-AC_CHECK_LIB(z,gzopen)
+AC_CHECK_LIB(z, gzopen)
+dnl AC_CHECK_LIB(bz2, BZ2_bzReadOpen)
 
 AC_OUTPUT(Makefile)
index 5dca051..f4e9486 100644 (file)
@@ -345,6 +345,12 @@ main(int argc, char **argv)
 /*@=nullassign =readonlytrans@*/
 #endif
 
+#if HAVE_MCHECK_H && HAVE_MTRACE
+       /*@-noeffect@*/
+       mtrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
+       /*@=noeffect@*/
+#endif
+
 #ifdef LC_CTYPE
        setlocale(LC_CTYPE, ""); /* makes islower etc work for other langs */
 #endif
@@ -366,14 +372,13 @@ main(int argc, char **argv)
                magicfile = usermagic;
        else {
                if ((home = getenv("HOME")) != NULL) {
-                       if ((usermagic = malloc(strlen(home) + 8)) != NULL) {
-                               (void)strcpy(usermagic, home);
-                               (void)strcat(usermagic, "/.magic");
-                               if (stat(usermagic, &sb)<0) 
-                                       free(usermagic);
-                               else
-                                       magicfile = usermagic;
-                       }
+                       usermagic = xmalloc(strlen(home) + 8);
+                       (void)strcpy(usermagic, home);
+                       (void)strcat(usermagic, "/.magic");
+                       if (stat(usermagic, &sb)<0) 
+                               free(usermagic);
+                       else
+                               magicfile = usermagic;
                }
        }
 
@@ -418,11 +423,9 @@ main(int argc, char **argv)
                case 'i':
                        iflag++;
                        mime = malloc(strlen(magicfile) + sizeof(".mime"));
-                       if (mime != NULL) {
-                               (void)strcpy(mime, magicfile);
-                               (void)strcat(mime, ".mime");
-                               magicfile = mime;
-                       }
+                       (void)strcpy(mime, magicfile);
+                       (void)strcat(mime, ".mime");
+                       magicfile = mime;
                        /*@switchbreak@*/ break;
                case 'k':
                        kflag = 1;
@@ -484,5 +487,10 @@ main(int argc, char **argv)
                        process(argv[optind], wid);
        }
 
+#if HAVE_MCHECK_H && HAVE_MTRACE
+       /*@-noeffect@*/
+       muntrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
+       /*@=noeffect@*/
+#endif
        return 0;
 }
index 3f935c9..acf84b9 100644 (file)
@@ -32,7 +32,7 @@
 FILE_RCSID("@(#)Id: softmagic.c,v 1.51 2002/07/03 18:26:38 christos Exp ")
 
 static int32_t
-mprint(union VALUETYPE *p, struct magic *m)
+sm_print(union VALUETYPE *p, struct magic *m)
        /*@globals fileSystem @*/
        /*@modifies p, fileSystem @*/
 {
@@ -99,7 +99,7 @@ mprint(union VALUETYPE *p, struct magic *m)
                break;
 
        default:
-               error("invalid m->type (%d) in mprint().\n", m->type);
+               error("invalid m->type (%d) in sm_print().\n", m->type);
                /*@notreached@*/
        }
        return(t);
@@ -111,7 +111,7 @@ mprint(union VALUETYPE *p, struct magic *m)
  * (unless you have a better idea)
  */
 static int
-mconvert(union VALUETYPE *p, struct magic *m)
+sm_convert(union VALUETYPE *p, struct magic *m)
        /*@globals fileSystem @*/
        /*@modifies p, fileSystem @*/
 {
@@ -373,7 +373,7 @@ mconvert(union VALUETYPE *p, struct magic *m)
        case REGEX:
                return 1;
        default:
-               error("invalid type %d in mconvert().\n", m->type);
+               error("invalid type %d in sm_convert().\n", m->type);
                /*@notreached@*/
                return 0;
        }
@@ -381,18 +381,18 @@ mconvert(union VALUETYPE *p, struct magic *m)
 
 
 static void
-mdebug(int32_t offset, char *str, int len)
+sm_debug(int32_t offset, char *str, int len)
        /*@globals fileSystem @*/
        /*@modifies fileSystem @*/
 {
-       (void) fprintf(stderr, "mget @%d: ", offset);
+       (void) fprintf(stderr, "sm_get @%d: ", offset);
        showstr(stderr, (char *) str, len);
        (void) fputc('\n', stderr);
        (void) fputc('\n', stderr);
 }
 
 static int
-mget(union VALUETYPE *p, unsigned char *s, struct magic *m, int nbytes)
+sm_get(union VALUETYPE *p, unsigned char *s, struct magic *m, int nbytes)
        /*@globals fileSystem @*/
        /*@modifies p, s, fileSystem @*/
 {
@@ -427,7 +427,7 @@ mget(union VALUETYPE *p, unsigned char *s, struct magic *m, int nbytes)
 /*@=branchstate@*/
 
        if (debug) {
-               mdebug(offset, (char *) p, sizeof(union VALUETYPE));
+               sm_debug(offset, (char *) p, sizeof(union VALUETYPE));
                mdump(m);
        }
 
@@ -761,17 +761,17 @@ mget(union VALUETYPE *p, unsigned char *s, struct magic *m, int nbytes)
                memcpy(p, s + offset, sizeof(union VALUETYPE));
 
                if (debug) {
-                       mdebug(offset, (char *) p, sizeof(union VALUETYPE));
+                       sm_debug(offset, (char *) p, sizeof(union VALUETYPE));
                        mdump(m);
                }
        }
-       if (!mconvert(p, m))
+       if (!sm_convert(p, m))
          return 0;
        return 1;
 }
 
 static int
-mcheck(union VALUETYPE *p, struct magic *m)
+sm_check(union VALUETYPE *p, struct magic *m)
        /*@globals fileSystem @*/
        /*@modifies fileSystem @*/
 {
@@ -872,7 +872,7 @@ mcheck(union VALUETYPE *p, struct magic *m)
        }
                /*@notreached@*/ break;
        default:
-               error("invalid type %d in mcheck().\n", m->type);
+               error("invalid type %d in sm_check().\n", m->type);
                /*@notreached@*/
                return 0;
        }
@@ -947,7 +947,7 @@ mcheck(union VALUETYPE *p, struct magic *m)
 
        default:
                matched = 0;
-               error("mcheck: can't happen: invalid relation %d.\n", m->reln);
+               error("sm_check: can't happen: invalid relation %d.\n", m->reln);
                /*@notreached@*/ break;
        }
 
@@ -982,7 +982,7 @@ mcheck(union VALUETYPE *p, struct magic *m)
  *     so that higher-level continuations are processed.
  */
 static int
-match(struct magic *m, uint32_t nmagic, unsigned char *s, int nbytes)
+sm_match(struct magic *m, uint32_t nmagic, unsigned char *s, int nbytes)
        /*@globals fileSystem @*/
        /*@modifies m, s, fileSystem @*/
 {
@@ -997,14 +997,10 @@ match(struct magic *m, uint32_t nmagic, unsigned char *s, int nbytes)
        int returnval = 0; /* if a match is found it is set to 1*/
        int firstline = 1; /* a flag to print X\n  X\n- X */
 
-       if (tmpoff == NULL)
-               if ((tmpoff = (int32_t *) malloc(tmplen = 20)) == NULL)
-                       error("out of memory\n");
-
        for (magindex = 0; magindex < nmagic; magindex++) {
                /* if main entry matches, print it... */
-               if (!mget(&p, s, &m[magindex], nbytes) ||
-                   !mcheck(&p, &m[magindex])) {
+               if (!sm_get(&p, s, &m[magindex], nbytes) ||
+                   !sm_check(&p, &m[magindex])) {
                            /* 
                             * main entry didn't match,
                             * flush its continuations
@@ -1016,80 +1012,69 @@ match(struct magic *m, uint32_t nmagic, unsigned char *s, int nbytes)
                }
 
                if (! firstline) { /* we found another match */
-                       /* put a newline and '-' to do some simple formatting*/
+                       /* put a newline and '-' to do some simple formatting */
                        printf("\n- ");
                }
 
-               tmpoff[cont_level] = mprint(&p, &m[magindex]);
+               if ((cont_level+1) >= tmplen)
+                       tmpoff = (int32_t *) xrealloc(tmpoff, tmplen += 20);
+               tmpoff[cont_level] = sm_print(&p, &m[magindex]);
+               cont_level++;
+
                /*
                 * If we printed something, we'll need to print
                 * a blank before we print something else.
                 */
                if (m[magindex].desc[0])
                        need_separator = 1;
-               /* and any continuations that match */
-               if (++cont_level >= tmplen)
-                       if ((tmpoff = (int32_t *) realloc(tmpoff,
-                                                      tmplen += 20)) == NULL)
-                               error("out of memory\n");
-               while (m[magindex+1].cont_level != 0 && 
-                      ++magindex < nmagic) {
-                       if (cont_level >= m[magindex].cont_level) {
-                               if (cont_level > m[magindex].cont_level) {
-                                       /*
-                                        * We're at the end of the level
-                                        * "cont_level" continuations.
-                                        */
-                                       cont_level = m[magindex].cont_level;
-                               }
-                               if (m[magindex].flag & OFFADD) {
-                                       oldoff=m[magindex].offset;
-                                       m[magindex].offset +=
-                                           tmpoff[cont_level-1];
-                               }
-                               if (mget(&p, s, &m[magindex], nbytes) &&
-                                   mcheck(&p, &m[magindex])) {
-                                       /*
-                                        * This continuation matched.
-                                        * Print its message, with
-                                        * a blank before it if
-                                        * the previous item printed
-                                        * and this item isn't empty.
-                                        */
-                                       /* space if previous printed */
-                                       if (need_separator
-                                          && (m[magindex].nospflag == 0)
-                                          && (m[magindex].desc[0] != '\0')
-                                          ) {
-                                               (void) putchar(' ');
-                                               need_separator = 0;
-                                       }
-                                       tmpoff[cont_level] =
-                                           mprint(&p, &m[magindex]);
-                                       if (m[magindex].desc[0])
-                                               need_separator = 1;
 
-                                       /*
-                                        * If we see any continuations
-                                        * at a higher level,
-                                        * process them.
-                                        */
-                                       if (++cont_level >= tmplen)
-                                               if ((tmpoff = 
-                                                   (int32_t *) realloc(tmpoff,
-                                                   tmplen += 20)) == NULL)
-                                                       error("out of memory\n");
-                               }
-                               if (m[magindex].flag & OFFADD) {
-                                        m[magindex].offset = oldoff;
+               /* and any continuations that match */
+               while (m[magindex+1].cont_level != 0 && ++magindex < nmagic) {
+                       if (cont_level < m[magindex].cont_level)
+                               continue;
+                       if (cont_level > m[magindex].cont_level) {
+                               /*
+                                * We're at the end of the level
+                                * "cont_level" continuations.
+                                */
+                               cont_level = m[magindex].cont_level;
+                       }
+                       if (m[magindex].flag & OFFADD) {
+                               oldoff = m[magindex].offset;
+                               m[magindex].offset += tmpoff[cont_level-1];
+                       }
+                       if (sm_get(&p, s, &m[magindex], nbytes) &&
+                           sm_check(&p, &m[magindex]))
+                       {
+                               /*
+                                * This continuation matched.
+                                * Print its message, with
+                                * a blank before it if
+                                * the previous item printed
+                                * and this item isn't empty.
+                                */
+                               /* space if previous printed */
+                               if (need_separator
+                                  && (m[magindex].nospflag == 0)
+                                  && (m[magindex].desc[0] != '\0')
+                                  ) {
+                                       (void) putchar(' ');
+                                       need_separator = 0;
                                }
+                               if ((cont_level+1) >= tmplen)
+                                       tmpoff = xrealloc(tmpoff, tmplen += 20);
+                               tmpoff[cont_level] = sm_print(&p, &m[magindex]);
+                               cont_level++;
+                               if (m[magindex].desc[0])
+                                       need_separator = 1;
                        }
+                       if (m[magindex].flag & OFFADD)
+                                m[magindex].offset = oldoff;
                }
                firstline = 0;
                returnval = 1;
-               if (!kflag) {
-                       return 1; /* don't keep searching */
-               }                       
+               if (!kflag)     /* don't keep searching */
+                       return 1;
        }
        return returnval;  /* This is hit if -k is set or there is no match */
 }
@@ -1105,7 +1090,7 @@ softmagic(unsigned char *buf, int nbytes)
        struct mlist *ml;
 
        for (ml = mlist.next; ml != &mlist; ml = ml->next) {
-               if (match(ml->magic, ml->nmagic, buf, nbytes))
+               if (sm_match(ml->magic, ml->nmagic, buf, nbytes))
                        return 1;
        }
 
index a705558..e7fe901 100644 (file)
@@ -190,6 +190,69 @@ extern char *sys_errlist[];
 #define strtoul(a, b, c)       strtol(a, b, c)
 #endif
 
+/*@-declundef -incondefs @*/
+/**
+ */
+/*@mayexit@*/ /*@only@*/ /*@out@*/
+void * xmalloc (size_t size)
+       /*@globals errno @*/
+       /*@ensures maxSet(result) == (size - 1) @*/
+       /*@modifies errno @*/;
+
+/**
+ */
+/*@mayexit@*/ /*@only@*/
+void * xcalloc (size_t nmemb, size_t size)
+       /*@ensures maxSet(result) == (nmemb - 1) @*/
+       /*@*/;
+
+/**
+ * @todo Annotate ptr with returned/out.
+ */
+/*@mayexit@*/ /*@only@*/
+void * xrealloc (/*@null@*/ /*@only@*/ void * ptr, size_t size)
+       /*@ensures maxSet(result) == (size - 1) @*/
+       /*@modifies *ptr @*/;
+
+/**
+ */
+/*@mayexit@*/ /*@only@*/
+char * xstrdup (const char *str)
+       /*@*/;
+/*@=declundef =incondefs @*/
+
+#if HAVE_MCHECK_H
+#include <mcheck.h>
+#if defined(__LCLINT__)
+/*@-declundef -incondefs @*/ /* LCL: missing annotations */
+extern int mcheck (void (*__abortfunc) (enum mcheck_status)) __THROW
+       /*@globals internalState@*/
+       /*@modifies internalState @*/;
+extern int mcheck_pedantic (void (*__abortfunc) (enum mcheck_status)) __THROW
+       /*@globals internalState@*/
+       /*@modifies internalState @*/;
+extern void mcheck_check_all (void)
+       /*@globals internalState@*/
+       /*@modifies internalState @*/;
+extern enum mcheck_status mprobe (void *__ptr) __THROW
+       /*@globals internalState@*/
+       /*@modifies internalState @*/;
+extern void mtrace (void) __THROW
+       /*@globals internalState@*/
+       /*@modifies internalState @*/;
+extern void muntrace (void) __THROW
+       /*@globals internalState@*/
+       /*@modifies internalState @*/;
+/*@=declundef =incondefs @*/
+#endif /* defined(__LCLINT__) */
+#endif /* HAVE_MCHECK_H */
+
+/* Memory allocation via macro defs to get meaningful locations from mtrace() */
+#define        xmalloc(_size)          (malloc(_size) ? : (error("out of memory"), NULL))
+#define        xcalloc(_nmemb, _size)  (calloc((_nmemb), (_size)) ? : (error("out of memory"), NULL))
+#define        xrealloc(_ptr, _size)   (realloc((_ptr), (_size)) ? : (error("out of memory"), NULL))
+#define        xstrdup(_str)   (strcpy(xmalloc(strlen(_str)+1), (_str)))
+
 #if HAVE_LOCALE_H
 # include <locale.h>
 #endif