[libxml2] Fix memory leak with invalid XSD



commit a09c89545d3ed5b56701abcc5d638faa01c5c903
Author: Nick Wellnhofer <wellnhofer aevum de>
Date:   Mon Aug 15 12:19:25 2022 +0200

    Fix memory leak with invalid XSD
    
    xmlSchemaClearElemInfo can add new items to the "matcher" cache, so the
    cache must be cleared after calling this function, not before. This
    only seems to affect invalid XSDs.
    
    Fixes #390.

 xmlschemas.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)
---
diff --git a/xmlschemas.c b/xmlschemas.c
index c1c8f4ef..f31d3d1f 100644
--- a/xmlschemas.c
+++ b/xmlschemas.c
@@ -27821,17 +27821,6 @@ xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
        } while (cur != NULL);
        vctxt->aidcs = NULL;
     }
-    if (vctxt->idcMatcherCache != NULL) {
-       xmlSchemaIDCMatcherPtr matcher = vctxt->idcMatcherCache, tmp;
-
-       while (matcher) {
-           tmp = matcher;
-           matcher = matcher->nextCached;
-           xmlSchemaIDCFreeMatcherList(tmp);
-       }
-       vctxt->idcMatcherCache = NULL;
-    }
-
 
     if (vctxt->idcNodes != NULL) {
        int i;
@@ -27898,6 +27887,21 @@ xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
         xmlFree(vctxt->filename);
        vctxt->filename = NULL;
     }
+
+    /*
+     * Note that some cleanup functions can move items to the cache,
+     * so the cache shouldn't be freed too early.
+     */
+    if (vctxt->idcMatcherCache != NULL) {
+       xmlSchemaIDCMatcherPtr matcher = vctxt->idcMatcherCache, tmp;
+
+       while (matcher) {
+           tmp = matcher;
+           matcher = matcher->nextCached;
+           xmlSchemaIDCFreeMatcherList(tmp);
+       }
+       vctxt->idcMatcherCache = NULL;
+    }
 }
 
 /**


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