diff --git a/libavformat/utils.c b/libavformat/utils.c
index 50a30f694232f99c0a984d630693d626ccd186f9..d5dfca7dec13614db97ea82d74fa71f5b2a91848 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -1981,7 +1981,7 @@ int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
         m         = (a + b) >> 1;
 
         // Search for the next non-discarded packet.
-        while ((entries[m].flags & AVINDEX_DISCARD_FRAME) && m < b) {
+        while ((entries[m].flags & AVINDEX_DISCARD_FRAME) && m < b && m < nb_entries - 1) {
             m++;
             if (m == b && entries[m].timestamp >= wanted_timestamp) {
                 m = b - 1;