diff --git a/libavformat/rmdec.c b/libavformat/rmdec.c
index 28fa62c4d2..0be9afe82c 100644
--- a/libavformat/rmdec.c
+++ b/libavformat/rmdec.c
@@ -459,6 +459,8 @@ static int rm_read_index(AVFormatContext *s)
         }
 
         for (n = 0; n < n_pkts; n++) {
+            if (avio_feof(pb))
+                return AVERROR_INVALIDDATA;
             avio_skip(pb, 2);
             pts = avio_rb32(pb);
             pos = avio_rb32(pb);