From 89a86169a6b96ccd3829c5600b667ec40bb56e47 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Fri, 27 Aug 2021 19:20:42 +0200 Subject: [PATCH] mp4: Fix possible memory leak on errors. 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 | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/mp4.c b/mp4.c index 1c33a176..372d5f49 100644 --- 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) { -- 2.39.2