diff --git a/libavcodec/xpmdec.c b/libavcodec/xpmdec.c
index 8d5bc76ab28600bb42e7e0dd311618cb2183e819..6cd7bde4ec7295a16b862c3541f60969715ed100 100644
--- a/libavcodec/xpmdec.c
+++ b/libavcodec/xpmdec.c
@@ -309,7 +309,10 @@ static int xpm_decode_frame(AVCodecContext *avctx, void *data,
     avctx->pix_fmt = AV_PIX_FMT_BGRA;
 
     end = avpkt->data + avpkt->size;
-    if (memcmp(ptr, "/* XPM */", 9)) {
+    while (memcmp(ptr, "/* XPM */\n", 10) && ptr < end - 10)
+        ptr++;
+
+    if (ptr >= end) {
         av_log(avctx, AV_LOG_ERROR, "missing signature\n");
         return AVERROR_INVALIDDATA;
     }