From 5ed19fbaa7145db070142c288ec5eb184d36dbc5 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Fri, 27 Aug 2021 15:38:25 +0200 Subject: [PATCH] mp4: Improve mp4_get_sample_size(). Use an unsigned type for the sample number and check that the passed number is within range. Since the function can fail now, let it return int and return the sample size via an additional pointer argument. --- aac_afh.c | 8 ++++---- mp4.c | 10 +++++++--- mp4.h | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/aac_afh.c b/aac_afh.c index b45db477..de99613d 100644 --- a/aac_afh.c +++ b/aac_afh.c @@ -90,7 +90,7 @@ static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context, const char **buf, uint32_t *len) { struct aac_afh_context *c = afh_context; - int32_t ss; + uint32_t ss; size_t offset; int ret; @@ -98,9 +98,9 @@ static int aac_afh_get_chunk(uint32_t chunk_num, void *afh_context, if (ret < 0) return ret; offset = c->fpos; - ss = mp4_get_sample_size(c->mp4, chunk_num); - if (ss <= 0) - return -E_MP4_BAD_SAMPLE; + ret = mp4_get_sample_size(c->mp4, chunk_num, &ss); + if (ret < 0) + return ret; assert(ss + offset <= c->mapsize); *buf = c->map + offset; *len = ss; diff --git a/mp4.c b/mp4.c index 869b80fa..205286a2 100644 --- a/mp4.c +++ b/mp4.c @@ -680,13 +680,17 @@ int mp4_set_sample_position(struct mp4 *f, uint32_t sample) return 1; } -int32_t mp4_get_sample_size(const struct mp4 *f, int sample) +int mp4_get_sample_size(const struct mp4 *f, uint32_t sample, uint32_t *result) { const struct mp4_track *t = &f->track; + if (sample >= t->stsz_sample_count) + return -ERRNO_TO_PARA_ERROR(EINVAL); if (t->stsz_sample_size != 0) - return t->stsz_sample_size; - return t->stsz_table[sample]; + *result = t->stsz_sample_size; + else + *result = t->stsz_table[sample]; + return 1; } uint32_t mp4_get_sample_rate(const struct mp4 *f) diff --git a/mp4.h b/mp4.h index ceb8dde2..15cc6af9 100644 --- a/mp4.h +++ b/mp4.h @@ -22,7 +22,7 @@ struct mp4; /* opaque */ int mp4_set_sample_position(struct mp4 *f, uint32_t sample); int mp4_open_read(const struct mp4_callback *cb, struct mp4 **result); void mp4_close(struct mp4 *f); -int32_t mp4_get_sample_size(const struct mp4 *f, int sample); +int mp4_get_sample_size(const struct mp4 *f, uint32_t sample, uint32_t *result); uint32_t mp4_get_sample_rate(const struct mp4 *f); uint32_t mp4_get_channel_count(const struct mp4 * f); int32_t mp4_num_samples(const struct mp4 *f); -- 2.39.2