From 643b4e90ebf619432b0287010b593edd8c0c0f8e Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Thu, 16 Feb 2023 14:45:06 +0100 Subject: [PATCH] malloc-fail: Fix infinite loop in htmlParseStartTag Found with libFuzzer, see #344. --- HTMLparser.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/HTMLparser.c b/HTMLparser.c index a53f7083..04578e70 100644 --- a/HTMLparser.c +++ b/HTMLparser.c @@ -2566,6 +2566,7 @@ htmlSkipBogusComment(htmlParserCtxtPtr ctxt) { static const xmlChar * htmlParseHTMLName(htmlParserCtxtPtr ctxt) { + const xmlChar *ret; int i = 0; xmlChar loc[HTML_PARSER_BUFFER_SIZE]; @@ -2583,7 +2584,11 @@ htmlParseHTMLName(htmlParserCtxtPtr ctxt) { NEXT; } - return(xmlDictLookup(ctxt->dict, loc, i)); + ret = xmlDictLookup(ctxt->dict, loc, i); + if (ret == NULL) + htmlErrMemory(ctxt, NULL); + + return(ret); } @@ -4031,7 +4036,8 @@ htmlParseStartTag(htmlParserCtxtPtr ctxt) { SKIP_BLANKS; while ((CUR != 0) && (CUR != '>') && - ((CUR != '/') || (NXT(1) != '>'))) { + ((CUR != '/') || (NXT(1) != '>')) && + (ctxt->instate != XML_PARSER_EOF)) { GROW; attname = htmlParseAttribute(ctxt, &attvalue); if (attname != NULL) {