diff --git a/libavformat/avs.c b/libavformat/avs.c
index 7b90f851530aa438b8bb7edfda72343f3c3952fc..ad5ac05988d3a925a9116fb2ebf4e4a6f29b28c2 100644
--- a/libavformat/avs.c
+++ b/libavformat/avs.c
@@ -169,6 +169,8 @@ static int avs_read_packet(AVFormatContext * s, AVPacket * pkt)
 
             switch (type) {
             case AVS_PALETTE:
+                if (size - 4 > sizeof(palette))
+                    return AVERROR_INVALIDDATA;
                 ret = avio_read(s->pb, palette, size - 4);
                 if (ret < size - 4)
                     return AVERROR(EIO);