[FFmpeg-devel] [PATCH] fix possible crash if vpre before vcodec

Reimar Döffinger Reimar.Doeffinger
Fri Feb 27 11:25:26 CET 2009

I recently tried the following command
ffmpeg -i baldursgate-logo.mve -vpre hq -vcodec libx264 -b 500k -an out.mp4
and wondered why it would not work.
strace showed the following:
open("/home/reimar/.ffmpeg/hq.ffpreset", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/home/reimar/.ffmpeg/(null)-hq.ffpreset", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/local/share/ffmpeg/hq.ffpreset", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/local/share/ffmpeg/(null)-hq.ffpreset", O_RDONLY) = -1 ENOENT (No such file or directory)
obviously ffmpeg tries to open the file already during option parsing (this could probably be considered
the real bug). When -vcodec is set only afterwards it passes NULL to
snprintf %s argument which may (and will) crash with some
I propose a patch that at least avoids the crash, but e.g. this:
-vcodec blub -vpre hq -vcodec libx264
will still have the counter-intuitive behaviour of reading
blub-hq.ffpreset but encoding with libx264.

diff --git a/ffmpeg.c b/ffmpeg.c
index 1e31570..1ca0dd9 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -3708,12 +3708,12 @@ static int opt_preset(const char *opt, const char *arg)
     for(i=!base[0]; i<2 && !f; i++){
+        char *codec_name= *opt == 'v' ? video_codec_name :
+                          *opt == 'a' ? audio_codec_name :
+                                        subtitle_codec_name;
         snprintf(filename, sizeof(filename), "%s%s/%s.ffpreset", base[i], i ? "" : "/.ffmpeg", arg);
         f= fopen(filename, "r");
-        if(!f){
-            char *codec_name= *opt == 'v' ? video_codec_name :
-                              *opt == 'a' ? audio_codec_name :
-                                            subtitle_codec_name;
+        if(!f && codec_name){
             snprintf(filename, sizeof(filename), "%s%s/%s-%s.ffpreset", base[i],  i ? "" : "/.ffmpeg", codec_name, arg);
             f= fopen(filename, "r");

More information about the ffmpeg-devel mailing list