fftools/ffmpeg_mux_init: Fix double-free on error
authorAndreas Rheinhardt <andreas.rheinhardt@outlook.com>
Mon, 25 Mar 2024 15:54:25 +0000 (16:54 +0100)
committerMichael Niedermayer <michael@niedermayer.cc>
Sun, 23 Nov 2025 03:32:15 +0000 (04:32 +0100)
MATCH_PER_STREAM_OPT iterates over all options of a given
OptionDef and tests whether they apply to the current stream;
if so, they are set to ost->apad, otherwise, the code errors
out. If no error happens, ost->apad is av_strdup'ed in order
to take ownership of this pointer.

But this means that setting it originally was premature,
as it leads to double-frees when an error happens lateron.
This can simply be reproduced with
ffmpeg -filter_complex anullsrc  -apad bar -apad:n baz -f null -
This is a regression since 83ace80bfd80fcdba2c65fa1d554923ea931d5bd.

Fix this by using a temporary variable instead of directly
setting ost->apad. Also only strdup the string if it actually
is != NULL.

Reviewed-by: Marth64 <marth64@proxyid.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
(cherry picked from commit ced5c5fdb8634d39ca9472a2026b2d2fea16c4e5)
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
(cherry picked from commit 8a1ccbd5dd76fb12ad75528038a9f7f50fee330d)
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
fftools/ffmpeg_opt.c

index 0969b12e7f46962d1cc327fb6afe60f6c0f625c9..65b4cc3cd85c4a757503f037ffd8b0b13b8d122f 100644 (file)
@@ -2054,6 +2054,7 @@ static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc, in
         int channels = 0;
         char *layout = NULL;
         char *sample_fmt = NULL;
+        const char *apad = NULL;
 
         MATCH_PER_STREAM_OPT(audio_channels, i, channels, oc, st);
         if (channels) {
@@ -2091,8 +2092,12 @@ static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc, in
 
         MATCH_PER_STREAM_OPT(audio_sample_rate, i, audio_enc->sample_rate, oc, st);
 
-        MATCH_PER_STREAM_OPT(apad, str, ost->apad, oc, st);
-        ost->apad = av_strdup(ost->apad);
+        MATCH_PER_STREAM_OPT(apad, str, apad, oc, st);
+        if (apad) {
+            ost->apad = av_strdup(apad);
+            if (!ost->apad)
+                exit_program(1);
+        }
 
         ost->avfilter = get_ost_filters(o, oc, ost);
         if (!ost->avfilter)