]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
mp4: Fix possible memory leak on errors.
authorAndre Noll <maan@tuebingen.mpg.de>
Fri, 27 Aug 2021 17:20:42 +0000 (19:20 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Mon, 27 Jun 2022 14:45:56 +0000 (16:45 +0200)
If the sanity checks in open_file() fail, we free the mp4 structure
but not the various tables and metadata items we might already have
allocated at this point.

Fix this by calling mp4_close() instead of freeing the mp4 struct
directly. We have to move mp4_close() above open_file() to avoid a
forward declaration.

mp4.c

diff --git a/mp4.c b/mp4.c
index 1c33a176e2fafd1d174017cea25edcfba536a5fb..372d5f4929f29422cb156262df6a7146c8829c77 100644 (file)
--- a/mp4.c
+++ b/mp4.c
@@ -567,6 +567,21 @@ static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only)
        return 1;
 }
 
+void mp4_close(struct mp4 *f)
+{
+       free(f->track.stsz_table);
+       free(f->track.stts_sample_count);
+       free(f->track.stsc_first_chunk);
+       free(f->track.stsc_samples_per_chunk);
+       free(f->track.stco_chunk_offset);
+       for (uint32_t n = 0; n < f->meta.count; n++) {
+               free(f->meta.tags[n].item);
+               free(f->meta.tags[n].value);
+       }
+       free(f->meta.tags);
+       free(f);
+}
+
 static int open_file(const struct mp4_callback *cb, bool meta_only, struct mp4 **result)
 {
        int ret;
@@ -599,7 +614,7 @@ static int open_file(const struct mp4_callback *cb, bool meta_only, struct mp4 *
        return 1;
 fail:
        *result = NULL;
-       free(f);
+       mp4_close(f);
        return ret;
 }
 
@@ -622,21 +637,6 @@ fail:
        return ret;
 }
 
-void mp4_close(struct mp4 *f)
-{
-       free(f->track.stsz_table);
-       free(f->track.stts_sample_count);
-       free(f->track.stsc_first_chunk);
-       free(f->track.stsc_samples_per_chunk);
-       free(f->track.stco_chunk_offset);
-       for (uint32_t n = 0; n < f->meta.count; n++) {
-               free(f->meta.tags[n].item);
-               free(f->meta.tags[n].value);
-       }
-       free(f->meta.tags);
-       free(f);
-}
-
 static int32_t chunk_of_sample(const struct mp4 *f, int32_t sample,
                int32_t *chunk)
 {