From a296ecdb1ab1657d3ec756fbb4cdd9618b621a2e Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Fri, 27 Aug 2021 15:12:34 +0200 Subject: [PATCH] afh: Limit chunk numbers to 32 bit. The number of chunks and the chunk offsets are stored in the audio file table as 32 bit unsigned integers. Thus, chunk numbers and sizes cannot exceed 2^32 - 1. Make this fact obvious by changing the corresponding parameters of aac_afh_get_chunk() from size_t or unsigned long to uint32_t. --- aac_afh.c | 19 +++++++++---------- afh.c | 4 ++-- afh.h | 6 +++--- afh_common.c | 4 ++-- afh_recv.c | 11 ++++++----- vss.c | 14 +++++++------- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/aac_afh.c b/aac_afh.c index 026df50f..5c1225b6 100644 --- a/aac_afh.c +++ b/aac_afh.c @@ -35,13 +35,13 @@ struct aac_afh_context { static uint32_t aac_afh_read_cb(void *user_data, void *dest, uint32_t want) { struct aac_afh_context *c = user_data; - uint32_t have, rv; + size_t have, rv; if (want == 0 || c->fpos >= c->mapsize) return 0; have = c->mapsize - c->fpos; - rv = PARA_MIN(have, want); - PARA_DEBUG_LOG("reading %u bytes @%zu\n", rv, c->fpos); + rv = PARA_MIN(have, (size_t)want); + PARA_DEBUG_LOG("reading %zu bytes @%zu\n", rv, c->fpos); memcpy(dest, c->map + c->fpos, rv); c->fpos += rv; return rv; @@ -126,8 +126,8 @@ static void aac_afh_close(void *afh_context) */ int32_t mp4ff_set_sample_position(mp4ff_t *f, const int32_t track, const int32_t sample); -static int aac_afh_get_chunk(long unsigned chunk_num, void *afh_context, - const char **buf, size_t *len) +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; @@ -166,8 +166,7 @@ static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd, struct aac_afh_context *c; int64_t tmp; const char *buf; - size_t sz; - uint32_t n; + uint32_t n, len; ret = aac_afh_open(map, numbytes, (void **)&c); if (ret < 0) @@ -192,16 +191,16 @@ static int aac_get_file_info(char *map, size_t numbytes, __a_unused int fd, afhi->chunks_total = rv; afhi->max_chunk_size = 0; for (n = 0; n < afhi->chunks_total; n++) { - if (aac_afh_get_chunk(n, c, &buf, &sz) < 0) + if (aac_afh_get_chunk(n, c, &buf, &len) < 0) break; - afhi->max_chunk_size = PARA_MAX((size_t)afhi->max_chunk_size, sz); + afhi->max_chunk_size = PARA_MAX(afhi->max_chunk_size, len); } tmp = c->masc.sbr_present_flag == 1? 2048 : 1024; afhi->seconds_total = tmp * afhi->chunks_total / afhi->frequency; ms2tv(1000 * tmp / afhi->frequency, &afhi->chunk_tv); - if (aac_afh_get_chunk(0, c, &buf, &sz) >= 0) + if (aac_afh_get_chunk(0, c, &buf, &len) >= 0) numbytes -= buf - map; afhi->bitrate = 8 * numbytes / afhi->seconds_total / 1000; _aac_afh_get_taginfo(c->mp4ff, &afhi->tags); diff --git a/afh.c b/afh.c index c896a7d1..e419d270 100644 --- a/afh.c +++ b/afh.c @@ -159,7 +159,7 @@ static void print_chunk_table(struct afh_info *afhi, int audio_format_id, struct timeval tv; long unsigned from, to; const char *buf; - size_t len; + uint32_t len; tv_scale(i, &afhi->chunk_tv, &tv); from = tv2ms(&tv); tv_scale(i + 1, &afhi->chunk_tv, &tv); @@ -177,7 +177,7 @@ static void print_chunk_table(struct afh_info *afhi, int audio_format_id, printf("%td - %td", buf - (const char *)map, buf + len - (const char *)map); if (!OPT_GIVEN(PARSER_FRIENDLY)) - printf(" (%zu)", len); + printf(" (%u)", len); printf("\n"); } afh_close(ctx, audio_format_id); diff --git a/afh.h b/afh.h index b3295f6e..ba72d80e 100644 --- a/afh.h +++ b/afh.h @@ -111,8 +111,8 @@ struct audio_format_handler { * portion of the memory mapped audio file. The caller must not call * free() on it. */ - int (*get_chunk)(long unsigned chunk_num, void *afh_context, - const char **buf, size_t *len); + int (*get_chunk)(uint32_t chunk_num, void *afh_context, + const char **buf, uint32_t *len); /** Deallocate the resources occupied by ->open(). */ void (*close)(void *afh_context); /** @@ -131,7 +131,7 @@ int compute_afhi(const char *path, char *data, size_t size, const char *audio_format_name(int); __must_check int afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi, uint8_t audio_format_id, const void *map, size_t mapsize, - const char **buf, size_t *len, void **afh_context); + const char **buf, uint32_t *len, void **afh_context); void afh_close(void *afh_context, uint8_t audio_format_id); int32_t afh_get_start_chunk(int32_t approx_chunk_num, const struct afh_info *afhi, uint8_t audio_format_id); diff --git a/afh_common.c b/afh_common.c index a267f58b..7e8f63d2 100644 --- a/afh_common.c +++ b/afh_common.c @@ -219,7 +219,7 @@ void clear_afhi(struct afh_info *afhi) free(afhi->tags.comment); } -static inline size_t get_chunk_len(long unsigned chunk_num, +static inline uint32_t get_chunk_len(long unsigned chunk_num, const struct afh_info *afhi) { return afhi->chunk_table[chunk_num + 1] - afhi->chunk_table[chunk_num]; @@ -247,7 +247,7 @@ static inline size_t get_chunk_len(long unsigned chunk_num, */ __must_check int afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi, uint8_t audio_format_id, const void *map, size_t mapsize, - const char **buf, size_t *len, void **afh_context) + const char **buf, uint32_t *len, void **afh_context) { struct audio_format_handler *afh = afl[audio_format_id]; diff --git a/afh_recv.c b/afh_recv.c index 4f8ff497..6a0ec239 100644 --- a/afh_recv.c +++ b/afh_recv.c @@ -174,6 +174,7 @@ static int afh_recv_post_select(__a_unused struct sched *s, void *context) char *buf; const char *start; size_t size; + uint32_t len; struct timeval chunk_time; unsigned j_given = RECV_CMD_OPT_GIVEN(AFH, JUST_IN_TIME, lpr); unsigned H_given = RECV_CMD_OPT_GIVEN(AFH, NO_HEADER, lpr); @@ -197,12 +198,12 @@ static int afh_recv_post_select(__a_unused struct sched *s, void *context) long unsigned n; for (n = pard->first_chunk; n < pard->last_chunk; n++) { ret = afh_get_chunk(n, afhi, pard->audio_format_num, - pard->map, pard->map_size, &start, &size, + pard->map, pard->map_size, &start, &len, &pard->afh_context); if (ret < 0) goto out; - PARA_DEBUG_LOG("adding %zu bytes\n", size); - btr_add_output_dont_free(start, size, btrn); + PARA_DEBUG_LOG("adding %u bytes\n", len); + btr_add_output_dont_free(start, len, btrn); } ret = -E_RECV_EOF; goto out; @@ -218,12 +219,12 @@ static int afh_recv_post_select(__a_unused struct sched *s, void *context) } ret = afh_get_chunk(pard->current_chunk, afhi, pard->audio_format_num, pard->map, - pard->map_size, &start, &size, + pard->map_size, &start, &len, &pard->afh_context); if (ret < 0) goto out; PARA_DEBUG_LOG("adding chunk %u\n", pard->current_chunk); - btr_add_output_dont_free(start, size, btrn); + btr_add_output_dont_free(start, len, btrn); if (pard->current_chunk >= pard->last_chunk) { ret = -E_RECV_EOF; goto out; diff --git a/vss.c b/vss.c index a4dfa94e..eb51a1b3 100644 --- a/vss.c +++ b/vss.c @@ -348,7 +348,7 @@ static int initialize_fec_client(struct fec_client *fc, struct vss_task *vsst) } static int vss_get_chunk(int chunk_num, struct vss_task *vsst, - char **buf, size_t *sz) + char **buf, uint32_t *len) { int ret; @@ -363,15 +363,15 @@ static int vss_get_chunk(int chunk_num, struct vss_task *vsst, if (chunk_num == 0 && vsst->header_len > 0) { assert(vsst->header_buf); *buf = vsst->header_buf; /* stripped header */ - *sz = vsst->header_len; + *len = vsst->header_len; return 0; } ret = afh_get_chunk(chunk_num, &mmd->afd.afhi, mmd->afd.audio_format_id, vsst->map, vsst->mapsize, - (const char **)buf, sz, &vsst->afh_context); + (const char **)buf, len, &vsst->afh_context); if (ret < 0) { *buf = NULL; - *sz = 0; + *len = 0; } return ret; } @@ -380,7 +380,7 @@ static int compute_group_size(struct vss_task *vsst, struct fec_group *g, int max_bytes) { char *buf; - size_t len; + uint32_t len; int ret, i, max_chunks = PARA_MAX(1LU, 150 / tv2ms(vss_chunk_time())); if (g->first_chunk == 0) { @@ -587,7 +587,7 @@ static int setup_next_fec_group(struct fec_client *fc, struct vss_task *vsst) slice_copied = 0; for (c = g->first_chunk; c < g->first_chunk + g->num_chunks; c++) { char *buf; - size_t src_len; + uint32_t src_len; ret = vss_get_chunk(c, vsst, &buf, &src_len); if (ret < 0) return ret; @@ -1030,7 +1030,7 @@ static void vss_send(struct vss_task *vsst) struct timeval due; struct fec_client *fc, *tmp_fc; char *buf; - size_t len; + uint32_t len; if (!vsst->map || !vss_playing()) return; -- 2.39.2