[libxml2] Add more checks for malloc failures in xmllint.c



commit 18d1f9d42ad52e0ce36f13e1dd73430dcf75c867
Author: Nick Wellnhofer <wellnhofer aevum de>
Date:   Tue Feb 1 15:56:21 2022 +0100

    Add more checks for malloc failures in xmllint.c
    
    Also fix a few of the existing checks.
    
    Fixes #197.
    Fixes #198.

 xmllint.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 84 insertions(+), 28 deletions(-)
---
diff --git a/xmllint.c b/xmllint.c
index 4ad80a0f..ae70a68d 100644
--- a/xmllint.c
+++ b/xmllint.c
@@ -1659,6 +1659,11 @@ testSAX(const char *filename) {
        xmlSchemaValidCtxtPtr vctxt;
 
        vctxt = xmlSchemaNewValidCtxt(wxschemas);
+        if (vctxt == NULL) {
+            progresult = XMLLINT_ERR_MEM;
+            xmlFreeParserInputBuffer(buf);
+            goto error;
+        }
        xmlSchemaSetValidErrors(vctxt, xmlGenericError, xmlGenericError, NULL);
        xmlSchemaValidateSetFilename(vctxt, filename);
 
@@ -1687,6 +1692,7 @@ testSAX(const char *filename) {
         */
        ctxt = xmlNewParserCtxt();
        if (ctxt == NULL) {
+            progresult = XMLLINT_ERR_MEM;
            xmlFreeParserInputBuffer(buf);
            goto error;
        }
@@ -2213,6 +2219,12 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
             if (res > 0) {
                 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
                             chars, res, filename, XML_CHAR_ENCODING_NONE);
+                if (ctxt == NULL) {
+                    progresult = XMLLINT_ERR_MEM;
+                    if (f != stdin)
+                        fclose(f);
+                    return;
+                }
                 htmlCtxtUseOptions(ctxt, options);
                 while ((res = fread(chars, 1, pushsize, f)) > 0) {
                     htmlParseChunk(ctxt, chars, res, 0);
@@ -2221,7 +2233,8 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
                 doc = ctxt->myDoc;
                 htmlFreeParserCtxt(ctxt);
             }
-            fclose(f);
+            if (f != stdin)
+                fclose(f);
         }
     }
 #endif /* LIBXML_PUSH_ENABLED */
@@ -2284,6 +2297,12 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
                if (res > 0) {
                    ctxt = xmlCreatePushParserCtxt(NULL, NULL,
                                chars, res, filename);
+                    if (ctxt == NULL) {
+                        progresult = XMLLINT_ERR_MEM;
+                        if (f != stdin)
+                            fclose(f);
+                        return;
+                    }
                    xmlCtxtUseOptions(ctxt, options);
                    while ((res = fread(chars, 1, size, f)) > 0) {
                        xmlParseChunk(ctxt, chars, res, 0);
@@ -2328,23 +2347,25 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
        } else if (htmlout) {
            xmlParserCtxtPtr ctxt;
 
-           if (rectxt == NULL)
+           if (rectxt == NULL) {
                ctxt = xmlNewParserCtxt();
-           else
-               ctxt = rectxt;
-           if (ctxt == NULL) {
-               doc = NULL;
-           } else {
-               ctxt->sax->error = xmlHTMLError;
-               ctxt->sax->warning = xmlHTMLWarning;
-               ctxt->vctxt.error = xmlHTMLValidityError;
-               ctxt->vctxt.warning = xmlHTMLValidityWarning;
+                if (ctxt == NULL) {
+                    progresult = XMLLINT_ERR_MEM;
+                    return;
+                }
+            } else {
+                ctxt = rectxt;
+            }
 
-               doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
+            ctxt->sax->error = xmlHTMLError;
+            ctxt->sax->warning = xmlHTMLWarning;
+            ctxt->vctxt.error = xmlHTMLValidityError;
+            ctxt->vctxt.warning = xmlHTMLValidityWarning;
 
-               if (rectxt == NULL)
-                   xmlFreeParserCtxt(ctxt);
-           }
+            doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
+
+            if (rectxt == NULL)
+                xmlFreeParserCtxt(ctxt);
 #ifdef HAVE_MMAP
        } else if (memory) {
            int fd;
@@ -2376,20 +2397,22 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
        } else if (valid) {
            xmlParserCtxtPtr ctxt = NULL;
 
-           if (rectxt == NULL)
+           if (rectxt == NULL) {
                ctxt = xmlNewParserCtxt();
-           else
+                if (ctxt == NULL) {
+                    progresult = XMLLINT_ERR_MEM;
+                    return;
+                }
+            } else {
                ctxt = rectxt;
-           if (ctxt == NULL) {
-               doc = NULL;
-           } else {
-               doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
+            }
 
-               if (ctxt->valid == 0)
-                   progresult = XMLLINT_ERR_RDFILE;
-               if (rectxt == NULL)
-                   xmlFreeParserCtxt(ctxt);
-           }
+            doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
+
+            if (ctxt->valid == 0)
+                progresult = XMLLINT_ERR_RDFILE;
+            if (rectxt == NULL)
+                xmlFreeParserCtxt(ctxt);
 #endif /* LIBXML_VALID_ENABLED */
        } else {
            if (rectxt != NULL)
@@ -2771,7 +2794,9 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
            if ((cvp = xmlNewValidCtxt()) == NULL) {
                xmlGenericError(xmlGenericErrorContext,
                        "Couldn't allocate validation context\n");
-               exit(-1);
+                progresult = XMLLINT_ERR_MEM;
+                xmlFreeDtd(dtd);
+                return;
            }
            cvp->userData = NULL;
            cvp->error    = xmlGenericError;
@@ -2803,7 +2828,9 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
        if ((cvp = xmlNewValidCtxt()) == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                    "Couldn't allocate validation context\n");
-           exit(-1);
+            progresult = XMLLINT_ERR_MEM;
+            xmlFreeDoc(doc);
+            return;
        }
 
        if ((timing) && (!repeat)) {
@@ -2840,6 +2867,11 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
        if (noout)
            flag |= XML_SCHEMATRON_OUT_QUIET;
        ctxt = xmlSchematronNewValidCtxt(wxschematron, flag);
+        if (ctxt == NULL) {
+            progresult = XMLLINT_ERR_MEM;
+            xmlFreeDoc(doc);
+            return;
+        }
 #if 0
        xmlSchematronSetValidErrors(ctxt, xmlGenericError, xmlGenericError,
                 NULL);
@@ -2873,6 +2905,11 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
        }
 
        ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
+        if (ctxt == NULL) {
+            progresult = XMLLINT_ERR_MEM;
+            xmlFreeDoc(doc);
+            return;
+        }
        xmlRelaxNGSetValidErrors(ctxt, xmlGenericError, xmlGenericError, NULL);
        ret = xmlRelaxNGValidateDoc(ctxt, doc);
        if (ret == 0) {
@@ -2900,6 +2937,11 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
        }
 
        ctxt = xmlSchemaNewValidCtxt(wxschemas);
+        if (ctxt == NULL) {
+            progresult = XMLLINT_ERR_MEM;
+            xmlFreeDoc(doc);
+            return;
+        }
        xmlSchemaSetValidErrors(ctxt, xmlGenericError, xmlGenericError, NULL);
        ret = xmlSchemaValidateDoc(ctxt, doc);
        if (ret == 0) {
@@ -3581,6 +3623,10 @@ main(int argc, char **argv) {
            startTimer();
        }
        ctxt = xmlSchematronNewParserCtxt(schematron);
+        if (ctxt == NULL) {
+            progresult = XMLLINT_ERR_MEM;
+            goto error;
+        }
 #if 0
        xmlSchematronSetParserErrors(ctxt, xmlGenericError, xmlGenericError,
                 NULL);
@@ -3613,6 +3659,10 @@ main(int argc, char **argv) {
            startTimer();
        }
        ctxt = xmlRelaxNGNewParserCtxt(relaxng);
+        if (ctxt == NULL) {
+            progresult = XMLLINT_ERR_MEM;
+            goto error;
+        }
        xmlRelaxNGSetParserErrors(ctxt, xmlGenericError, xmlGenericError,
                 NULL);
        relaxngschemas = xmlRelaxNGParse(ctxt);
@@ -3637,6 +3687,10 @@ main(int argc, char **argv) {
            startTimer();
        }
        ctxt = xmlSchemaNewParserCtxt(schema);
+        if (ctxt == NULL) {
+            progresult = XMLLINT_ERR_MEM;
+            goto error;
+        }
        xmlSchemaSetParserErrors(ctxt, xmlGenericError, xmlGenericError, NULL);
        wxschemas = xmlSchemaParse(ctxt);
        if (wxschemas == NULL) {
@@ -3804,6 +3858,8 @@ main(int argc, char **argv) {
     if (patternc != NULL)
         xmlFreePattern(patternc);
 #endif
+
+error:
     xmlCleanupParser();
     xmlMemoryDump();
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]