[FFmpeg-devel] [PATCH 4/5] avformat/avformat: Avoid av_strdup(NULL)

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Sat Feb 3 21:33:29 EET 2024


It is not documented to be safe.
Also copy these lists in a more generic manner.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavformat/avformat.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/libavformat/avformat.c b/libavformat/avformat.c
index 9cacaef87d..ae79ee6ef7 100644
--- a/libavformat/avformat.c
+++ b/libavformat/avformat.c
@@ -875,20 +875,28 @@ const AVCodec *ff_find_decoder(AVFormatContext *s, const AVStream *st,
 
 int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
 {
+#define OFF(field) offsetof(AVFormatContext, field)
+    static const unsigned offsets[] = {
+        OFF(codec_whitelist),    OFF(format_whitelist),
+        OFF(protocol_whitelist), OFF(protocol_blacklist),
+    };
+#undef OFF
     av_assert0(!dst->codec_whitelist &&
                !dst->format_whitelist &&
                !dst->protocol_whitelist &&
                !dst->protocol_blacklist);
-    dst-> codec_whitelist = av_strdup(src->codec_whitelist);
-    dst->format_whitelist = av_strdup(src->format_whitelist);
-    dst->protocol_whitelist = av_strdup(src->protocol_whitelist);
-    dst->protocol_blacklist = av_strdup(src->protocol_blacklist);
-    if (   (src-> codec_whitelist && !dst-> codec_whitelist)
-        || (src->  format_whitelist && !dst->  format_whitelist)
-        || (src->protocol_whitelist && !dst->protocol_whitelist)
-        || (src->protocol_blacklist && !dst->protocol_blacklist)) {
-        av_log(dst, AV_LOG_ERROR, "Failed to duplicate black/whitelist\n");
-        return AVERROR(ENOMEM);
+    for (unsigned i = 0; i < FF_ARRAY_ELEMS(offsets); i++) {
+        const char *src_str = *(char *const*)((const char*)src + offsets[i]);
+
+        if (src_str) {
+            char *dst_str = av_strdup(src_str);
+            if (!dst_str) {
+                av_log(dst, AV_LOG_ERROR, "Failed to duplicate black/whitelist\n");
+                return AVERROR(ENOMEM);
+            }
+
+            *(char **)((char*)dst + offsets[i]) = dst_str;
+        }
     }
     return 0;
 }
-- 
2.34.1



More information about the ffmpeg-devel mailing list