Blame SOURCES/0001-fix-RESOURCE_LEAK-errors-detected-by-covscan-in-src-.patch

b52212
From 6ef06c19bb4e0a410f523bdae8c675229408ad17 Mon Sep 17 00:00:00 2001
b52212
From: Coiby Xu <coiby.xu@gmail.com>
b52212
Date: Fri, 11 Jun 2021 11:00:27 +0800
b52212
Subject: [PATCH 1/8] fix RESOURCE_LEAK errors detected by covscan in
b52212
 src/xmlpythonizer.c
b52212
b52212
Fix the following errors found by covscan,
b52212
b52212
    Error: RESOURCE_LEAK (CWE-772): [#def6]
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:847: alloc_fn: Storage is returned from allocation function "malloc".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:847: var_assign: Assigning: "key" = storage returned from "malloc(258UL)".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:853: noescape: Resource "key" is not freed or pointed-to in "_get_key_value".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:857: leaked_storage: Variable "key" going out of scope leaks the storage it points to.
b52212
    #  855|                           PyADD_DICT_VALUE(retdata, key, value);
b52212
    #  856|                   } else {
b52212
    #  857|->                         PyReturnError(PyExc_ValueError, "Could not get key value: %s [%i] (Defining key: %s)",
b52212
    #  858|                                         map_p->rootpath, elmtid, map_p->key);
b52212
    #  859|                   }
b52212
b52212
    Error: RESOURCE_LEAK (CWE-772): [#def7]
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:847: alloc_fn: Storage is returned from allocation function "malloc".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:847: var_assign: Assigning: "key" = storage returned from "malloc(258UL)".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:879: noescape: Resource "key" is not freed or pointed-to in "_get_key_value".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:921: leaked_storage: Variable "key" going out of scope leaks the storage it points to.
b52212
    #  919|                                   xmlXPathFreeObject(xpo);
b52212
    #  920|                           } else {
b52212
    #  921|->                                 PyReturnError(PyExc_ValueError, "Could not get key value: "
b52212
    #  922|                                                 "%s [%i] (Defining key: %s)",
b52212
    #  923|                                                 map_p->rootpath, elmtid, map_p->key);
b52212
b52212
    Error: RESOURCE_LEAK (CWE-772): [#def8]
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:877: alloc_fn: Storage is returned from allocation function "_get_xpath_values".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:877: var_assign: Assigning: "xpo" = storage returned from "_get_xpath_values(xpctx, map_p->value)".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:921: leaked_storage: Variable "xpo" going out of scope leaks the storage it points to.
b52212
    #  919|                                   xmlXPathFreeObject(xpo);
b52212
    #  920|                           } else {
b52212
    #  921|->                                 PyReturnError(PyExc_ValueError, "Could not get key value: "
b52212
    #  922|                                                 "%s [%i] (Defining key: %s)",
b52212
    #  923|                                                 map_p->rootpath, elmtid, map_p->key);
b52212
b52212
    Error: RESOURCE_LEAK (CWE-772): [#def9]
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:847: alloc_fn: Storage is returned from allocation function "malloc".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:847: var_assign: Assigning: "key" = storage returned from "malloc(258UL)".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:933: noescape: Resource "key" is not freed or pointed-to in "_get_key_value".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:934: leaked_storage: Variable "key" going out of scope leaks the storage it points to.
b52212
    #  932|                   }
b52212
    #  933|                   if( _get_key_value(logp, key, 256, map_p, xpctx, 0) == NULL ) {
b52212
    #  934|->                         PyReturnError(PyExc_ValueError,
b52212
    #  935|                                         "Could not get key value: %s [%i] (Defining key: %s)",
b52212
    #  936|                                         map_p->rootpath, elmtid, map_p->key);
b52212
b52212
    Error: RESOURCE_LEAK (CWE-772): [#def10]
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:847: alloc_fn: Storage is returned from allocation function "malloc".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:847: var_assign: Assigning: "key" = storage returned from "malloc(258UL)".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:947: noescape: Resource "key" is not freed or pointed-to in "_get_key_value".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:948: leaked_storage: Variable "key" going out of scope leaks the storage it points to.
b52212
    #  946|                   }
b52212
    #  947|                   if( _get_key_value(logp, key, 256, map_p, xpctx, 0) == NULL ) {
b52212
    #  948|->                         PyReturnError(PyExc_ValueError,
b52212
    #  949|                                         "Could not get key value: %s [%i] (Defining key: %s)",
b52212
    #  950|                                         map_p->rootpath, elmtid, map_p->key);
b52212
b52212
    Error: RESOURCE_LEAK (CWE-772): [#def11]
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:847: alloc_fn: Storage is returned from allocation function "malloc".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:847: var_assign: Assigning: "key" = storage returned from "malloc(258UL)".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:947: identity_transfer: Passing "key" as argument 2 to function "_get_key_value", which returns that argument.
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:947: noescape: Resource "key" is not freed or pointed-to in "_get_key_value".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:959: leaked_storage: Variable "key" going out of scope leaks the storage it points to.
b52212
    #  957|                                   xmlXPathFreeObject(xpo);
b52212
    #  958|                           }
b52212
    #  959|->                         PyReturnError(PyExc_ValueError,
b52212
    #  960|                                         "Could not get key value: %s [%i] (Defining key: %s)",
b52212
    #  961|                                         map_p->rootpath, elmtid, map_p->key);
b52212
b52212
    Error: RESOURCE_LEAK (CWE-772): [#def12]
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:847: alloc_fn: Storage is returned from allocation function "malloc".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:847: var_assign: Assigning: "key" = storage returned from "malloc(258UL)".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:947: identity_transfer: Passing "key" as argument 2 to function "_get_key_value", which returns that argument.
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:947: noescape: Resource "key" is not freed or pointed-to in "_get_key_value".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:995: leaked_storage: Variable "key" going out of scope leaks the storage it points to.
b52212
    #  993|                           } else {
b52212
    #  994|                                   // If NULL, something is wrong - exception is already set.
b52212
    #  995|->                                 return NULL;
b52212
    #  996|                           }
b52212
    #  997|                   }
b52212
b52212
    Error: RESOURCE_LEAK (CWE-772): [#def13]
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:954: alloc_fn: Storage is returned from allocation function "_get_xpath_values".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:954: var_assign: Assigning: "xpo" = storage returned from "_get_xpath_values(xpctx, map_p->value)".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:995: leaked_storage: Variable "xpo" going out of scope leaks the storage it points to.
b52212
    #  993|                           } else {
b52212
    #  994|                                   // If NULL, something is wrong - exception is already set.
b52212
    #  995|->                                 return NULL;
b52212
    #  996|                           }
b52212
    #  997|                   }
b52212
b52212
    Error: RESOURCE_LEAK (CWE-772): [#def14]
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:1031: alloc_fn: Storage is returned from allocation function "malloc".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:1031: var_assign: Assigning: "key" = storage returned from "malloc(258UL)".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:1050: leaked_storage: Variable "key" going out of scope leaks the storage it points to.
b52212
    # 1048|                           xpctx = xmlXPathNewContext(xpdoc);
b52212
    # 1049|                           if( xpctx == NULL ) {
b52212
    # 1050|->                                 PyReturnError(PyExc_MemoryError, "Could not setup new XPath context");
b52212
    # 1051|                           }
b52212
    # 1052|                           xpctx->node = data_n;
b52212
b52212
    Error: RESOURCE_LEAK (CWE-772): [#def15]
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:1054: alloc_fn: Storage is returned from allocation function "_get_xpath_values".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:1054: var_assign: Assigning: "xpo" = storage returned from "_get_xpath_values(xpctx, map_p->rootpath)".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:1065: leaked_storage: Variable "xpo" going out of scope leaks the storage it points to.
b52212
    # 1063|                                                           // Exit if we get NULL - something is wrong
b52212
    # 1064|                                                           //and exception is set
b52212
    # 1065|->                                                         return NULL;
b52212
    # 1066|                                                   }
b52212
    # 1067|                                           }
b52212
b52212
    Error: RESOURCE_LEAK (CWE-772): [#def16]
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:1031: alloc_fn: Storage is returned from allocation function "malloc".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:1031: var_assign: Assigning: "key" = storage returned from "malloc(258UL)".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:1059: identity_transfer: Passing "key" as argument 2 to function "_get_key_value", which returns that argument.
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:1059: noescape: Resource "key" is not freed or pointed-to in "_get_key_value".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:1059: identity_transfer: Passing "key" as argument 2 to function "_get_key_value", which returns that argument.
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:1059: noescape: Resource "key" is not freed or pointed-to in "_get_key_value".
b52212
    python-dmidecode-3.12.2/src/xmlpythonizer.c:1087: leaked_storage: Variable "key" going out of scope leaks the storage it points to.
b52212
    # 1085|                                   // Exit if we get NULL - something is wrong
b52212
    # 1086|                                   //and exception is set
b52212
    # 1087|->                                 return NULL;
b52212
    # 1088|                           }
b52212
    # 1089|                   }
b52212
b52212
Signed-off-by: Coiby Xu <coiby.xu@gmail.com>
b52212
---
b52212
 src/xmlpythonizer.c | 27 +++++++++++++++++++++++++++
b52212
 1 file changed, 27 insertions(+)
b52212
b52212
diff --git a/src/xmlpythonizer.c b/src/xmlpythonizer.c
b52212
index e9c9242..ce2d067 100644
b52212
--- a/src/xmlpythonizer.c
b52212
+++ b/src/xmlpythonizer.c
b52212
@@ -854,6 +854,9 @@ PyObject *_deep_pythonize(Log_t *logp, PyObject *retdata,
b52212
                         value = PyBytes_FromString(map_p->value);
b52212
                         PyADD_DICT_VALUE(retdata, key, value);
b52212
                 } else {
b52212
+                        xmlXPathFreeContext(xpctx);
b52212
+                        xmlFreeDoc(xpdoc);
b52212
+                        free(key);
b52212
                         PyReturnError(PyExc_ValueError, "Could not get key value: %s [%i] (Defining key: %s)",
b52212
                                       map_p->rootpath, elmtid, map_p->key);
b52212
                 }
b52212
@@ -918,6 +921,10 @@ PyObject *_deep_pythonize(Log_t *logp, PyObject *retdata,
b52212
                                 PyADD_DICT_VALUE(retdata, key, value);
b52212
                                 xmlXPathFreeObject(xpo);
b52212
                         } else {
b52212
+                                xmlXPathFreeObject(xpo);
b52212
+                                xmlXPathFreeContext(xpctx);
b52212
+                                xmlFreeDoc(xpdoc);
b52212
+                                free(key);
b52212
                                 PyReturnError(PyExc_ValueError, "Could not get key value: "
b52212
                                               "%s [%i] (Defining key: %s)",
b52212
                                               map_p->rootpath, elmtid, map_p->key);
b52212
@@ -931,6 +938,9 @@ PyObject *_deep_pythonize(Log_t *logp, PyObject *retdata,
b52212
                         break;
b52212
                 }
b52212
                 if( _get_key_value(logp, key, 256, map_p, xpctx, 0) == NULL ) {
b52212
+                        xmlXPathFreeContext(xpctx);
b52212
+                        xmlFreeDoc(xpdoc);
b52212
+                        free(key);
b52212
                         PyReturnError(PyExc_ValueError,
b52212
                                       "Could not get key value: %s [%i] (Defining key: %s)",
b52212
                                       map_p->rootpath, elmtid, map_p->key);
b52212
@@ -945,6 +955,9 @@ PyObject *_deep_pythonize(Log_t *logp, PyObject *retdata,
b52212
                         break;
b52212
                 }
b52212
                 if( _get_key_value(logp, key, 256, map_p, xpctx, 0) == NULL ) {
b52212
+                        xmlXPathFreeContext(xpctx);
b52212
+                        xmlFreeDoc(xpdoc);
b52212
+                        free(key);
b52212
                         PyReturnError(PyExc_ValueError,
b52212
                                       "Could not get key value: %s [%i] (Defining key: %s)",
b52212
                                       map_p->rootpath, elmtid, map_p->key);
b52212
@@ -956,6 +969,9 @@ PyObject *_deep_pythonize(Log_t *logp, PyObject *retdata,
b52212
                         if( xpo != NULL ) {
b52212
                                 xmlXPathFreeObject(xpo);
b52212
                         }
b52212
+                        xmlXPathFreeContext(xpctx);
b52212
+                        xmlFreeDoc(xpdoc);
b52212
+                        free(key);
b52212
                         PyReturnError(PyExc_ValueError,
b52212
                                       "Could not get key value: %s [%i] (Defining key: %s)",
b52212
                                       map_p->rootpath, elmtid, map_p->key);
b52212
@@ -991,6 +1007,10 @@ PyObject *_deep_pythonize(Log_t *logp, PyObject *retdata,
b52212
                                         PyList_Append(value, dataset);
b52212
                                 }
b52212
                         } else {
b52212
+                                xmlXPathFreeObject(xpo);
b52212
+                                xmlXPathFreeContext(xpctx);
b52212
+                                xmlFreeDoc(xpdoc);
b52212
+                                free(key);
b52212
                                 // If NULL, something is wrong - exception is already set.
b52212
                                 return NULL;
b52212
                         }
b52212
@@ -1047,6 +1067,8 @@ PyObject *pythonizeXMLnode(Log_t *logp, ptzMAP *in_map, xmlNode *data_n) {
b52212
 
b52212
                         xpctx = xmlXPathNewContext(xpdoc);
b52212
                         if( xpctx == NULL ) {
b52212
+                                xmlFreeDoc(xpdoc);
b52212
+                                free(key);
b52212
                                 PyReturnError(PyExc_MemoryError, "Could not setup new XPath context");
b52212
                         }
b52212
                         xpctx->node = data_n;
b52212
@@ -1062,6 +1084,10 @@ PyObject *pythonizeXMLnode(Log_t *logp, ptzMAP *in_map, xmlNode *data_n) {
b52212
                                                 if( res == NULL ) {
b52212
                                                         // Exit if we get NULL - something is wrong
b52212
                                                         //and exception is set
b52212
+                                                        xmlXPathFreeObject(xpo);
b52212
+                                                        xmlXPathFreeContext(xpctx);
b52212
+                                                        xmlFreeDoc(xpdoc);
b52212
+                                                        free(key);
b52212
                                                         return NULL;
b52212
                                                 }
b52212
                                         }
b52212
@@ -1084,6 +1110,7 @@ PyObject *pythonizeXMLnode(Log_t *logp, ptzMAP *in_map, xmlNode *data_n) {
b52212
                         if( res == NULL ) {
b52212
                                 // Exit if we get NULL - something is wrong
b52212
                                 //and exception is set
b52212
+                                free(key);
b52212
                                 return NULL;
b52212
                         }
b52212
                 }
b52212
-- 
b52212
2.31.1
b52212