diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c
index 0f8270a..1f3808b 100644
--- a/libavformat/mpeg.c
+++ b/libavformat/mpeg.c
@@ -305,7 +305,7 @@ static int mpegps_read_pes_header(AVFormatContext *s,
                 header_len -= 5;
             }
         }
-        if (flags & 0x01) { /* PES extension */
+        if (flags & 0x01 && header_len > 0) { /* PES extension */
             pes_ext = get_byte(&s->pb);
             header_len--;
             if (pes_ext & 0x40) { /* pack header - should be zero in PS */
