1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-11-23 21:54:53 +02:00

avutil/opt: Don't cast when the result might be misaligned

A pointer conversion is UB if the resulting pointer is not
correctly aligned for the resultant type, even if no
load/store is ever performed through that pointer (C11 6.3.2.3 (7)).

This may happen in opt_copy_elem(), because the pointers are
converted even when they belong to a type that does not guarantee
sufficient alignment.

Fix this by deferring the cast after having checked the type.
Also make the casts -Wcast-qual safe and avoid an indirection
for src.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt
2024-03-25 14:54:35 +01:00
parent aa7d6520e6
commit c85477f78d

View File

@@ -2028,18 +2028,19 @@ void *av_opt_ptr(const AVClass *class, void *obj, const char *name)
static int opt_copy_elem(void *logctx, enum AVOptionType type, static int opt_copy_elem(void *logctx, enum AVOptionType type,
void *dst, const void *src) void *dst, const void *src)
{ {
uint8_t **dst8 = (uint8_t **)dst;
const uint8_t **src8 = (const uint8_t **)src;
if (type == AV_OPT_TYPE_STRING) { if (type == AV_OPT_TYPE_STRING) {
if (*dst8 != *src8) const char *src_str = *(const char *const *)src;
av_freep(dst8); char **dstp = (char **)dst;
if (*src8) { if (*dstp != src_str)
*dst8 = av_strdup(*src8); av_freep(dstp);
if (!*dst8) if (src_str) {
*dstp = av_strdup(src_str);
if (!*dstp)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }
} else if (type == AV_OPT_TYPE_BINARY) { } else if (type == AV_OPT_TYPE_BINARY) {
const uint8_t *const *src8 = (const uint8_t *const *)src;
uint8_t **dst8 = (uint8_t **)dst;
int len = *(const int *)(src8 + 1); int len = *(const int *)(src8 + 1);
if (*dst8 != *src8) if (*dst8 != *src8)
av_freep(dst8); av_freep(dst8);
@@ -2052,12 +2053,12 @@ static int opt_copy_elem(void *logctx, enum AVOptionType type,
} else if (type == AV_OPT_TYPE_CONST) { } else if (type == AV_OPT_TYPE_CONST) {
// do nothing // do nothing
} else if (type == AV_OPT_TYPE_DICT) { } else if (type == AV_OPT_TYPE_DICT) {
AVDictionary **sdict = (AVDictionary **)src; const AVDictionary *sdict = *(const AVDictionary * const *)src;
AVDictionary **ddict = (AVDictionary **)dst; AVDictionary **ddictp = (AVDictionary **)dst;
if (*sdict != *ddict) if (sdict != *ddictp)
av_dict_free(ddict); av_dict_free(ddictp);
*ddict = NULL; *ddictp = NULL;
return av_dict_copy(ddict, *sdict, 0); return av_dict_copy(ddictp, sdict, 0);
} else if (type == AV_OPT_TYPE_CHLAYOUT) { } else if (type == AV_OPT_TYPE_CHLAYOUT) {
if (dst != src) if (dst != src)
return av_channel_layout_copy(dst, src); return av_channel_layout_copy(dst, src);