Permalink
Browse files

Fix memory leak (Anatol Belski)

  • Loading branch information...
zoulasc committed Feb 21, 2014
1 parent dcfaba6 commit c0c0032b9e9eb57b91fefef905a3b018bab492d9
Showing with 8 additions and 4 deletions.
  1. +8 −4 src/softmagic.c
View
@@ -32,7 +32,7 @@
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.175 2014/02/18 11:09:31 kim Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.176 2014/02/18 17:59:21 kim Exp $")
#endif /* lint */
#include "magic.h"
@@ -1762,12 +1762,16 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
ms->offset = soffset;
if (rv == 1) {
if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
- file_printf(ms, F(m->desc, "%u"), offset) == -1)
+ file_printf(ms, F(m->desc, "%u"), offset) == -1) {
+ free(rbuf);
return -1;
- if (file_printf(ms, "%s", rbuf) == -1)
+ }
+ if (file_printf(ms, "%s", rbuf) == -1) {
+ free(rbuf);
return -1;
- free(rbuf);
+ }
}
+ free(rbuf);

This comment has been minimized.

Show comment
Hide comment
@remicollet

remicollet Feb 26, 2014

I observe, in some run condition, than rbuf could be NULL, so this could raise a minor issue.
The path applied in PHP seems to work correctly with

    if (rbuf) {
        free(rbuf);
    }

Ok, free(NULL) is valid.

Would it be simpler to use:

    if (rv == 1) {
        if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
              file_printf(ms, m->desc, offset) == -1) {
            rv = -1;
        }
        else if (file_printf(ms, "%s", rbuf) == -1) {
            rv = -1;
        }
    }
    if (rbuf) {
        free(rbuf);
    }
    return rv;
@remicollet

remicollet Feb 26, 2014

I observe, in some run condition, than rbuf could be NULL, so this could raise a minor issue.
The path applied in PHP seems to work correctly with

    if (rbuf) {
        free(rbuf);
    }

Ok, free(NULL) is valid.

Would it be simpler to use:

    if (rv == 1) {
        if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
              file_printf(ms, m->desc, offset) == -1) {
            rv = -1;
        }
        else if (file_printf(ms, "%s", rbuf) == -1) {
            rv = -1;
        }
    }
    if (rbuf) {
        free(rbuf);
    }
    return rv;

This comment has been minimized.

Show comment
Hide comment
@glensc

glensc Feb 26, 2014

Member

@remicollet please read this mirror description:

NOTE: do not make pull requests here, nor comment any commits, submit them usual way to bug tracker or to the mailing list. Maintainer(s) are not tracking this git mirror.

@glensc

glensc Feb 26, 2014

Member

@remicollet please read this mirror description:

NOTE: do not make pull requests here, nor comment any commits, submit them usual way to bug tracker or to the mailing list. Maintainer(s) are not tracking this git mirror.

This comment has been minimized.

Show comment
Hide comment
@zoulasc

zoulasc Feb 26, 2014

Contributor
@zoulasc

zoulasc via email Feb 26, 2014

Contributor
return rv;
case FILE_USE:

0 comments on commit c0c0032

Please sign in to comment.