avcodec/vvcdec: allocate and store structs on their own within the table list

Fixes "runtime error: member access within misaligned address 0xf00 for type
'struct bar', which requires # byte alignment" errors under GCC ubsan.

Reviewed-by: Nuo Mi <nuomi2021@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
Signed-off-by: Paul B Mahol <onemda@gmail.com>
This commit is contained in:
James Almer 2024-01-09 09:16:09 -03:00 committed by Paul B Mahol
parent 5b4f25b44a
commit 5d2150afc1

View File

@ -55,14 +55,6 @@ typedef struct TabList {
l->nb_tabs++; \
} while (0)
static size_t tl_size(const TabList *l)
{
size_t total = 0;
for (int i = 0; i < l->nb_tabs; i++)
total += l->tabs[i].size;
return total;
}
static void tl_init(TabList *l, const int zero, const int realloc)
{
l->nb_tabs = 0;
@ -72,32 +64,28 @@ static void tl_init(TabList *l, const int zero, const int realloc)
static int tl_free(TabList *l)
{
for (int i = 1; i < l->nb_tabs; i++) {
void **p = l->tabs[i].tab;
*p = NULL;
}
av_freep(l->tabs[0].tab);
for (int i = 0; i < l->nb_tabs; i++)
av_freep(l->tabs[i].tab);
return 0;
}
static int tl_create(TabList *l)
{
size_t size = tl_size(l);
if (l->realloc) {
uint8_t *p = l->zero ? av_mallocz(size) : av_malloc(size);
if (!p)
return AVERROR(ENOMEM);
tl_free(l);
// set pointer for each table
for (int i = 0; i < l->nb_tabs; i++) {
Tab *t = l->tabs + i;
*t->tab = p;
p += t->size;
*t->tab = l->zero ? av_mallocz(t->size) : av_malloc(t->size);
if (!*t->tab)
return AVERROR(ENOMEM);
}
} else if (l->zero) {
for (int i = 0; i < l->nb_tabs; i++) {
Tab *t = l->tabs + i;
memset(*t->tab, 0, t->size);
}
} else {
if (l->zero)
memset(*l->tabs[0].tab, 0, size);
}
return 0;
}