diff --git a/libavcodec/4xm.c b/libavcodec/4xm.c
index fa5256c9ecdb5fb97e53b563887bc457780d9fc9..82f8417f0e16c284d87c32446f3291da2d9cafa7 100644
--- a/libavcodec/4xm.c
+++ b/libavcodec/4xm.c
@@ -721,6 +721,10 @@ static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length)
     }
 
     prestream = read_huffman_tables(f, prestream);
+    if (!prestream) {
+        av_log(f->avctx, AV_LOG_ERROR, "Error reading Huffman tables.\n");
+        return AVERROR_INVALIDDATA;
+    }
 
     init_get_bits(&f->gb, buf + 4, 8 * bitstream_size);