From: Christos Zoulas Date: Tue, 28 Oct 2014 22:25:17 +0000 (+0000) Subject: PR/394: Properly cleanup in new buffers function. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9c08053a5feb28610d5957cb0cfebc70d7922693;p=file PR/394: Properly cleanup in new buffers function. --- diff --git a/src/apprentice.c b/src/apprentice.c index 6426c4c1..df479561 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: apprentice.c,v 1.219 2014/10/28 22:10:46 christos Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.220 2014/10/28 22:25:17 christos Exp $") #endif /* lint */ #include "magic.h" @@ -577,7 +577,7 @@ protected int buffer_apprentice(struct magic_set *ms, struct magic **bufs, size_t *sizes, size_t nbufs) { - size_t i; + size_t i, j; struct mlist *ml; struct magic_map *map; @@ -593,29 +593,31 @@ buffer_apprentice(struct magic_set *ms, struct magic **bufs, mlist_free(ms->mlist[i]); if ((ms->mlist[i] = mlist_alloc()) == NULL) { file_oomem(ms, sizeof(*ms->mlist[i])); - while (i-- > 0) { - mlist_free(ms->mlist[i]); - ms->mlist[i] = NULL; - } - return -1; + goto fail; } } for (i = 0; i < nbufs; i++) { map = apprentice_buf(ms, bufs[i], sizes[i]); if (map == NULL) - return -1; + goto fail; - for (i = 0; i < MAGIC_SETS; i++) { - if (add_mlist(ms->mlist[i], map, i) == -1) { + for (j = 0; j < MAGIC_SETS; j++) { + if (add_mlist(ms->mlist[j], map, j) == -1) { file_oomem(ms, sizeof(*ml)); apprentice_unmap(map); - return -1; + goto fail; } } } return 0; +fail: + for (i = 0; i < MAGIC_SETS; i++) { + mlist_free(ms->mlist[i]); + ms->mlist[i] = NULL; + } + return -1; } #endif @@ -644,11 +646,9 @@ file_apprentice(struct magic_set *ms, const char *fn, int action) mlist_free(ms->mlist[i]); if ((ms->mlist[i] = mlist_alloc()) == NULL) { file_oomem(ms, sizeof(*ms->mlist[i])); - if (i != 0) { - --i; - do - mlist_free(ms->mlist[i]); - while (i != 0); + while (i-- > 0) { + mlist_free(ms->mlist[i]); + ms->mlist[i] = NULL; } free(mfn); return -1;