[FFmpeg-cvslog] avformat/avformat: Avoid av_strdup(NULL)

Andreas Rheinhardt git at videolan.org
Wed Feb 7 11:37:42 EET 2024


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Sat Feb  3 19:05:25 2024 +0100| [ad9f644505bddd8b7243d4d9927ca08d6f80e97a] | committer: Andreas Rheinhardt

avformat/avformat: Avoid av_strdup(NULL)

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>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=ad9f644505bddd8b7243d4d9927ca08d6f80e97a
---

 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;
 }



More information about the ffmpeg-cvslog mailing list