From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Subject: [FFmpeg-devel] [PATCH 12/12] avutil/opt: Don't cast when the result might be misaligned
Date: Mon, 25 Mar 2024 16:11:20 +0100
Message-ID: <GV1P250MB0737DD6DF8B0DF2F3FB6C58D8F362@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <GV1P250MB073784B8A168B66DF3CB720D8F362@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM>
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>
---
libavutil/opt.c | 29 +++++++++++++++--------------
1 file changed, 15 insertions(+), 14 deletions(-)
diff --git a/libavutil/opt.c b/libavutil/opt.c
index 3130606337..5fa8870707 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -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,
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 (*dst8 != *src8)
- av_freep(dst8);
- if (*src8) {
- *dst8 = av_strdup(*src8);
- if (!*dst8)
+ const char *src_str = *(const char *const *)src;
+ char **dstp = (char **)dst;
+ if (*dstp != src_str)
+ av_freep(dstp);
+ if (src_str) {
+ *dstp = av_strdup(src_str);
+ if (!*dstp)
return AVERROR(ENOMEM);
}
} 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);
if (*dst8 != *src8)
av_freep(dst8);
@@ -2052,12 +2053,12 @@ static int opt_copy_elem(void *logctx, enum AVOptionType type,
} else if (type == AV_OPT_TYPE_CONST) {
// do nothing
} else if (type == AV_OPT_TYPE_DICT) {
- AVDictionary **sdict = (AVDictionary **)src;
- AVDictionary **ddict = (AVDictionary **)dst;
- if (*sdict != *ddict)
- av_dict_free(ddict);
- *ddict = NULL;
- return av_dict_copy(ddict, *sdict, 0);
+ const AVDictionary *sdict = *(const AVDictionary * const *)src;
+ AVDictionary **ddictp = (AVDictionary **)dst;
+ if (sdict != *ddictp)
+ av_dict_free(ddictp);
+ *ddictp = NULL;
+ return av_dict_copy(ddictp, sdict, 0);
} else if (type == AV_OPT_TYPE_CHLAYOUT) {
if (dst != src)
return av_channel_layout_copy(dst, src);
--
2.40.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2024-03-25 15:11 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-25 1:51 [FFmpeg-devel] [PATCH 01/10] doc/examples: Always use <> includes Andreas Rheinhardt
2024-03-25 1:53 ` [FFmpeg-devel] [PATCH 02/10] avutil/fifo, file: Remove unused headers Andreas Rheinhardt
2024-03-26 16:44 ` Stefano Sabatini
2024-03-25 1:53 ` [FFmpeg-devel] [PATCH 03/10] avutil/common: Don't auto-include mem.h Andreas Rheinhardt
2024-03-25 21:18 ` Michael Niedermayer
2024-03-25 21:27 ` Andreas Rheinhardt
2024-03-25 1:53 ` [FFmpeg-devel] [PATCH 04/10] fftools/ffmpeg_sched: Explicitly return 0 on sch_enc_send() success Andreas Rheinhardt
2024-03-27 16:16 ` Andreas Rheinhardt
2024-03-25 1:53 ` [FFmpeg-devel] [PATCH 05/10] avcodec/wmaprodec: Explicitly return 0 on success Andreas Rheinhardt
2024-03-25 1:53 ` [FFmpeg-devel] [PATCH 06/10] avcodec/ratecontrol: Avoid function pointer casts Andreas Rheinhardt
2024-03-25 1:53 ` [FFmpeg-devel] [PATCH 07/10] avcodec/bfi: Remove unused AVCodecContext* from context Andreas Rheinhardt
2024-03-25 1:53 ` [FFmpeg-devel] [PATCH 08/10] avcodec/mjpegenc: Inline chroma subsampling Andreas Rheinhardt
2024-03-25 1:53 ` [FFmpeg-devel] [PATCH 09/10] avcodec/magicyuvenc: Don't call functions twice due to macro Andreas Rheinhardt
2024-03-25 1:53 ` [FFmpeg-devel] [PATCH 10/10] avcodec/profiles: Don't include avcodec.h Andreas Rheinhardt
2024-03-25 12:56 ` [FFmpeg-devel] [PATCH 11/11] avutil/opt: Avoid av_strdup(NULL) Andreas Rheinhardt
2024-03-25 15:11 ` Andreas Rheinhardt [this message]
2024-03-25 16:33 ` [FFmpeg-devel] [PATCH 13/13] fftools/ffmpeg_mux_init: Fix double-free on error Andreas Rheinhardt
2024-03-25 21:33 ` Marth64
2024-03-25 16:38 ` [FFmpeg-devel] [PATCH 14/14] avutil/avstring: Avoid av_strdup(NULL) Andreas Rheinhardt
2024-03-25 16:50 ` Vittorio Giovara
2024-03-25 16:55 ` Andreas Rheinhardt
2024-03-25 18:15 ` Vittorio Giovara
2024-03-25 18:18 ` James Almer
2024-03-25 18:24 ` Andreas Rheinhardt
2024-03-26 16:43 ` [FFmpeg-devel] [PATCH 01/10] doc/examples: Always use <> includes Stefano Sabatini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=GV1P250MB0737DD6DF8B0DF2F3FB6C58D8F362@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM \
--to=andreas.rheinhardt@outlook.com \
--cc=ffmpeg-devel@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git