diff --git a/libavformat/mov.c b/libavformat/mov.c
index acfbfc5324d138128af93ed795f9bc90426dfbf9..04567fc475cb1a100776d68079a6188208910484 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -3132,7 +3132,7 @@ static int find_prev_closest_index(AVStream *st,
             }
         }
 
-        while (*index >= 0 && (*ctts_index) >= 0) {
+        while (*index >= 0 && (*ctts_index) >= 0 && (*ctts_index) < ctts_count) {
             // Find a "key frame" with PTS <= timestamp_pts (So that we can decode B-frames correctly).
             // No need to add dts_shift to the timestamp here becase timestamp_pts has already been
             // compensated by dts_shift above.