diff mbox series

[FFmpeg-devel,16/25] avformat/utils: Don't allocate zero-sized array

Message ID AM7PR03MB666021EF9A6D82457376A33C8FC89@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit bcbd84f435f5f449c04f93023eb874e07c76470c
Headers show
Series [FFmpeg-devel,01/25] avformat/matroskadec: Fix heap-buffer overflow upon gigantic timestamps | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/configureppc warning Failed to apply patch

Commit Message

Andreas Rheinhardt Aug. 27, 2021, 2:27 p.m. UTC
It is unnecessary and also ill-defined: av_malloc() returns a 1-byte
block of memory in this case, but this is not documented.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavformat/utils.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/utils.c b/libavformat/utils.c
index 264b5b1a4b..b3ff9e95eb 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -3167,15 +3167,19 @@  static int compute_chapters_end(AVFormatContext *s)
 {
     unsigned int i;
     int64_t max_time = 0;
-    AVChapter **timetable = av_malloc(s->nb_chapters * sizeof(*timetable));
+    AVChapter **timetable;
 
-    if (!timetable)
-        return AVERROR(ENOMEM);
+    if (!s->nb_chapters)
+        return 0;
 
     if (s->duration > 0 && s->start_time < INT64_MAX - s->duration)
         max_time = s->duration +
                        ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
 
+    timetable = av_malloc(s->nb_chapters * sizeof(*timetable));
+    if (!timetable)
+        return AVERROR(ENOMEM);
+
     for (i = 0; i < s->nb_chapters; i++)
         timetable[i] = s->chapters[i];
     qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp);