From 61e73851a33f0b4cb7662f8578a4695e77bd3c19 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <michael@niedermayer.cc>
Date: Sat, 23 Dec 2023 18:04:32 +0100
Subject: [PATCH 3/4] avfilter/f_reverse: Apply PTS compensation only when pts
 is  available

Fixes: out of array access
Fixes: tickets/10753/poc16ffmpeg

Regression since: 45dc668aea0edac34969b5a1ff76cf9ad3a09be1
Found-by: Zeng Yunxiang
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>

CVE: CVE-2023-51796

Upstream-Status: Backport [https://github.com/ffmpeg/FFmpeg/commit/61e73851a33f0b4cb7662f8578a4695e77bd3c19]

Signed-off-by: Archana Polampalli <archana.polampalli@windriver.com>
---
 libavfilter/f_reverse.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libavfilter/f_reverse.c b/libavfilter/f_reverse.c
index f7a7e71..7b919d6 100644
--- a/libavfilter/f_reverse.c
+++ b/libavfilter/f_reverse.c
@@ -251,7 +251,9 @@ static int areverse_request_frame(AVFilterLink *outlink)
     if (ret == AVERROR_EOF && s->nb_frames > 0) {
         AVFrame *out = s->frames[s->nb_frames - 1];
         out->pts     = s->pts[s->flush_idx++] - s->nb_samples;
-        s->nb_samples += s->pts[s->flush_idx] - s->pts[s->flush_idx - 1] - out->nb_samples;
+        if (s->nb_frames > 1)
+            s->nb_samples += s->pts[s->flush_idx] - s->pts[s->flush_idx - 1] - out->nb_samples;
+

         if (av_sample_fmt_is_planar(out->format))
             reverse_samples_planar(out);
--
2.40.0
