diff mbox series

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

Message ID 20200907024952.11697-3-andreas.rheinhardt@gmail.com
State Superseded
Headers show
Series [FFmpeg-devel,01/10] avformat/segment: Don't overwrite AVCodecParameters after init | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Sept. 7, 2020, 2:49 a.m. UTC
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@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 mbox series

Patch

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