From ae342c5f78003b80b3c94d3f5f9fdaba6b735f69 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Tue, 3 Jun 2025 19:52:18 +0200 Subject: [PATCH 6/7] avcodec/huffman: Combine allocations Signed-off-by: Andreas Rheinhardt --- libavcodec/huffman.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/libavcodec/huffman.c b/libavcodec/huffman.c index 0de3097a82..0c2d0c27e1 100644 --- a/libavcodec/huffman.c +++ b/libavcodec/huffman.c @@ -39,7 +39,10 @@ #define HNODE -1 typedef struct HeapElem { - uint64_t val; + union { + uint64_t val; + uint16_t dummy; // exists solely to ensure alignof(HeapElem) >= alignof(uint16_t) + }; int name; } HeapElem; @@ -59,19 +62,23 @@ static void heap_sift(HeapElem *h, int root, int size) int ff_huff_gen_len_table(uint8_t *dst, const uint64_t *stats, int stats_size, int skip0) { - HeapElem *h = av_malloc_array(sizeof(*h), stats_size); - int *up = av_malloc_array(sizeof(*up) * 2, stats_size); - uint8_t *len = av_malloc_array(sizeof(*len) * 2, stats_size); - uint16_t *map= av_malloc_array(sizeof(*map), stats_size); + int *up; + uint16_t *map; + uint8_t *len; + HeapElem *h = av_malloc_array(stats_size, + sizeof(*h) + 2 * sizeof(up) + 2 * sizeof(len) + sizeof(map)); + if (!h) + return AVERROR(ENOMEM); + up = (int*)(h + stats_size); + // map is suitably aligned because up uses an even number of elements + // and alignof(uint16_t) is either 1 or 2. + map = (uint16_t*)(up + 2 * stats_size); + len = (uint8_t*)(map + stats_size); + int offset, i, next; int size = 0; int ret = 0; - if (!h || !up || !len || !map) { - ret = AVERROR(ENOMEM); - goto end; - } - for (i = 0; i