diff --git a/HTMLparser.c b/HTMLparser.c
index 19464639..946fe178 100644
--- a/HTMLparser.c
+++ b/HTMLparser.c
@@ -3706,7 +3706,8 @@ htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
htmlParseErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED,
"DOCTYPE improperly terminated\n", NULL, NULL);
/* Ignore bogus content */
- while ((CUR != 0) && (CUR != '>'))
+ while ((CUR != 0) && (CUR != '>') &&
+ (ctxt->instate != XML_PARSER_EOF))
NEXT;
}
if (CUR == '>')