Memory leak in apprentice.c

Brett Funderburg brettf at deepfile.com
Thu Oct 16 15:09:40 EDT 2003


Using valgrind --leak-check=yes on file-4.06, it reports that
apprentice.c line 227 is leaking memory.
It appears in this bit of code:

    if ((fn = mfn = strdup(fn)) == NULL) {
        file_oomem(ms);
        return NULL;
    }

mfn is a local variable and is never free'd after successful pass through
this function. Actually, it looks like mfn is *never* used in this
function, so it might be possible to eliminate this whole block using the
included patch.

--- apprentice.c.orig   2003-10-16 19:07:43.000000000 +0000
+++ apprentice.c    2003-10-16 19:09:17.000000000 +0000
@@ -215,7 +215,7 @@
 protected struct mlist *
 file_apprentice(struct magic_set *ms, const char *fn, int action)
 {
-   char *p, *mfn, *afn = NULL;
+   char *p, *afn = NULL;
    int file_err, errs = -1;
    struct mlist *mlist;

@@ -224,13 +224,7 @@
    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);
        return NULL;
    }
@@ -244,7 +238,6 @@
            break;
        if (ms->flags & MAGIC_MIME) {
            if ((afn = malloc(strlen(fn) + 5 + 1)) == NULL) {
-               free(mfn);
                free(mlist);
                file_oomem(ms);
                return NULL;
@@ -263,7 +256,6 @@
        fn = p;
    }
    if (errs == -1) {
-       free(mfn);
        free(mlist);
        mlist = NULL;
        file_error(ms, 0, "could not find any magic files!");


brett




More information about the File mailing list