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

[libvirt] [PATCH 4/6] domain_conf: Avoid false positive



Gcc fails to see that virDomainChrGetDomainPtrsInternal always
sets pointers to a non NULL value (except for
chr device type VIR_DOMAIN_CHR_DEVICE_TYPE_LAST which if occurs,
we are in way bigger problem than NULL deref).

In file included from conf/domain_conf.c:37:0:
conf/domain_conf.c: In function 'virDomainChrPreAlloc':
conf/domain_conf.c:14109:35: error: potential null pointer dereference [-Werror=null-dereference]
     return VIR_REALLOC_N(*arrPtr, *cntPtr + 1);
                                   ^~
./util/viralloc.h:158:73: note: in definition of macro 'VIR_REALLOC_N'
 # define VIR_REALLOC_N(ptr, count) virReallocN(&(ptr), sizeof(*(ptr)), (count), \
                                                                         ^~~~~
conf/domain_conf.c: In function 'virDomainChrRemove':
conf/domain_conf.c:14133:21: error: potential null pointer dereference [-Werror=null-dereference]
     for (i = 0; i < *cntPtr; i++) {
                     ^~~~~~~
cc1: all warnings being treated as errors

Signed-off-by: Michal Privoznik <mprivozn redhat com>
---
 src/conf/domain_conf.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 3daeb1e..67ed4d7 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -14106,6 +14106,12 @@ virDomainChrPreAlloc(virDomainDefPtr vmdef,
 
     virDomainChrGetDomainPtrsInternal(vmdef, chr->deviceType, &arrPtr, &cntPtr);
 
+    if (!arrPtr || !cntPtr) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Impossible happened"));
+        return -1;
+    }
+
     return VIR_REALLOC_N(*arrPtr, *cntPtr + 1);
 }
 
@@ -14118,6 +14124,12 @@ virDomainChrInsertPreAlloced(virDomainDefPtr vmdef,
 
     virDomainChrGetDomainPtrsInternal(vmdef, chr->deviceType, &arrPtr, &cntPtr);
 
+    if (!arrPtr || !cntPtr) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Impossible happened"));
+        return;
+    }
+
     VIR_APPEND_ELEMENT_INPLACE(*arrPtr, *cntPtr, chr);
 }
 
@@ -14130,6 +14142,12 @@ virDomainChrRemove(virDomainDefPtr vmdef,
 
     virDomainChrGetDomainPtrsInternal(vmdef, chr->deviceType, &arrPtr, &cntPtr);
 
+    if (!arrPtr || !cntPtr) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Impossible happened"));
+        return NULL;
+    }
+
     for (i = 0; i < *cntPtr; i++) {
         ret = (*arrPtr)[i];
 
-- 
2.8.3


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