From 09a7e73ecbb54dc8d4877aa28a4f5b0591475861 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Wed, 18 Aug 2021 18:08:38 +0200 Subject: [PATCH] mp4: Remove ->error of struct mp4. It's easier to have track_add(), the only function which sets ->error, return an integer error code instead. Since track_add() is simple and is only called by parse_sub_atoms(), open-code the logic there. Also, don't reset ->total_tracks on errors because this leads to a memory leak, don't increase the track counter on errors and remove the comment which only states what is obvious. --- mp4.c | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/mp4.c b/mp4.c index c40fbe0b..477b4307 100644 --- a/mp4.c +++ b/mp4.c @@ -52,11 +52,8 @@ struct mp4 { uint8_t last_atom; uint64_t file_size; - uint32_t error; - /* incremental track index while reading the file */ int32_t total_tracks; - /* track data */ struct mp4_track *track[MAX_TRACKS]; @@ -445,18 +442,6 @@ static int32_t set_position(struct mp4 *f, int64_t position) return 0; } -static void track_add(struct mp4 *f) -{ - f->total_tracks++; - - if (f->total_tracks > MAX_TRACKS) { - f->total_tracks = 0; - f->error++; - return; - } - f->track[f->total_tracks - 1] = para_calloc(sizeof(struct mp4_track)); -} - static int read_stsz(struct mp4 *f) { int ret; @@ -904,11 +889,13 @@ static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, int meta_only) if (size == 0) return -1; counted_size += size; - /* we're starting to read a new track, update index, - * so that all data and tables get written in the right place - */ - if (atom_type == ATOM_TRAK) - track_add(f); + if (atom_type == ATOM_TRAK) { + if (f->total_tracks >= MAX_TRACKS) + return -1; + f->total_tracks++; + f->track[f->total_tracks - 1] = para_calloc( + sizeof(struct mp4_track)); + } /* parse subatoms */ if (meta_only && !need_parse_when_meta_only(atom_type)) { set_position(f, get_position(f) + size - header_size); @@ -966,7 +953,7 @@ struct mp4 *mp4_open_read(const struct mp4_callback *cb) f->cb = cb; ret = parse_atoms(f, 0); - if (ret < 0 || f->error) { + if (ret < 0) { free(f); return NULL; } @@ -1168,7 +1155,7 @@ struct mp4 *mp4_open_meta(const struct mp4_callback *cb) f->cb = cb; ret = parse_atoms(f, 1); - if (ret < 0 || f->error) { + if (ret < 0) { free(f); return NULL; } -- 2.39.5