diff --git a/libavcodec/qdm2.c b/libavcodec/qdm2.c
index 1286cc5163897f9c55630ec77412ffbc6577de97..61ac0a830df3daebb87540df0ab06bbbc63a98c6 100644
--- a/libavcodec/qdm2.c
+++ b/libavcodec/qdm2.c
@@ -1232,6 +1232,11 @@ static void qdm2_decode_super_block (QDM2Context *q)
     for (i = 0; packet_bytes > 0; i++) {
         int j;
 
+        if (i >= FF_ARRAY_ELEMS(q->sub_packet_list_A)) {
+            SAMPLES_NEEDED_2("too many packet bytes");
+            return;
+        }
+
         q->sub_packet_list_A[i].next = NULL;
 
         if (i > 0) {