From 6676c08aba77219683f50c02ad7f9b0812a11749 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Wed, 18 Aug 2021 20:59:25 +0200 Subject: [PATCH] mp4: Provide return value for mp4_set_sample_position(). This function fails if the given parameters are invalid. Detect this and return EINVAL in this case. Add corresponding error checking to the aac audio format handler. --- aac_afh.c | 6 ++++-- mp4.c | 12 ++++++------ mp4.h | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/aac_afh.c b/aac_afh.c index 34ad7cba..f41b3c2b 100644 --- a/aac_afh.c +++ b/aac_afh.c @@ -103,9 +103,11 @@ static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context, struct aac_afh_context *c = afh_context; int32_t ss; size_t offset; + int ret; - assert(chunk_num <= INT_MAX); - mp4_set_sample_position(c->mp4, c->track, chunk_num); + ret = mp4_set_sample_position(c->mp4, c->track, chunk_num); + if (ret < 0) + return ret; offset = c->fpos; ss = mp4_get_sample_size(c->mp4, c->track, chunk_num); if (ss <= 0) diff --git a/mp4.c b/mp4.c index 89d90c73..2f9c105d 100644 --- a/mp4.c +++ b/mp4.c @@ -8,6 +8,7 @@ #include #include "para.h" +#include "error.h" #include "portable_io.h" #include "string.h" #include "mp4.h" @@ -988,9 +989,6 @@ static int32_t chunk_of_sample(const struct mp4 *f, int32_t track, *chunk_sample = 0; *chunk = 1; - if (f->track[track] == NULL) { - return -1; - } total_entries = f->track[track]->stsc_entry_count; @@ -1050,8 +1048,6 @@ static int32_t sample_range_size(const struct mp4 *f, int32_t track, if (t->stsz_sample_size) return (sample - chunk_sample) * t->stsz_sample_size; - if (sample >= t->stsz_sample_count) - return 0; /* error */ for (i = chunk_sample, total = 0; i < sample; i++) total += t->stsz_table[i]; return total; @@ -1094,14 +1090,18 @@ bool mp4_is_audio_track(const struct mp4 *f, int32_t track) return f->track[track]->is_audio; } -void mp4_set_sample_position(struct mp4 *f, int32_t track, int32_t sample) +int mp4_set_sample_position(struct mp4 *f, uint32_t track, int32_t sample) { + const struct mp4_track *t = f->track[track]; int32_t offset, chunk, chunk_sample; + if (sample >= t->stsz_sample_count || track >= f->total_tracks) + return -ERRNO_TO_PARA_ERROR(EINVAL); chunk_of_sample(f, track, sample, &chunk_sample, &chunk); offset = chunk_to_offset(f, track, chunk) + sample_range_size(f, track, chunk_sample, sample); set_position(f, offset); + return 1; } int32_t mp4_get_sample_size(const struct mp4 *f, int track, int sample) diff --git a/mp4.h b/mp4.h index 0bf74818..e7ca0d96 100644 --- a/mp4.h +++ b/mp4.h @@ -19,7 +19,7 @@ struct mp4_metadata { struct mp4; /* opaque */ -void mp4_set_sample_position(struct mp4 *f, int32_t track, int32_t sample); +int mp4_set_sample_position(struct mp4 *f, uint32_t track, int32_t sample); int32_t mp4_get_total_tracks(const struct mp4 *f); bool mp4_is_audio_track(const struct mp4 *f, int32_t track); struct mp4 *mp4_open_read(const struct mp4_callback *cb); -- 2.39.5