diff --git a/libavcodec/8bps.c b/libavcodec/8bps.c
index 95a35159e3..292aa03c6b 100644
--- a/libavcodec/8bps.c
+++ b/libavcodec/8bps.c
@@ -71,6 +71,9 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
     unsigned char *planemap = c->planemap;
     int ret;
 
+    if (buf_size < planes * height *2)
+        return AVERROR_INVALIDDATA;
+
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;