[FFmpeg-devel] [PATCH 03/10] avformat/segment: Fix segfault on allocation error, avoid allocation

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Mon Sep 7 05:49:45 EEST 2020


If the user has set none of the options specifying the segments'
durations, a default value of 2s is used by duplicating a "2" string and
using av_parse_time() on it. Yet duplicating the string was unchecked
and if the allocation failed, one would get a segfault in
av_parse_time().

This commit solves this by avoiding duplicating the string altogether if
no string has been provided; instead the duration is set explicitly to
what av_parse_time() would return for the string "2".

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
One could btw make the segment_time an option of type
AV_OPT_TYPE_DURATION, but then one could no longer distinguish the cases
in which the value has been set to its default value by the user and the
one in which the user hasn't set the option at all. Would this be a
problem?

Btw: Why are values <= 0 allowed for this?

 libavformat/segment.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libavformat/segment.c b/libavformat/segment.c
index 0c96c8c50c..9fafec0e35 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -704,9 +704,9 @@ static int seg_init(AVFormatContext *s)
             return ret;
     } else {
         /* set default value if not specified */
-        if (!seg->time_str)
-            seg->time_str = av_strdup("2");
-        if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
+        if (!seg->time_str) {
+            seg->time = 2000000;
+        } else if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
             av_log(s, AV_LOG_ERROR,
                    "Invalid time duration specification '%s' for segment_time option\n",
                    seg->time_str);
-- 
2.20.1



More information about the ffmpeg-devel mailing list