
On Thu, Jun 02, 2016 at 12:42:52 +0200, Michal Privoznik wrote:
There's this problem on the recent gcc-6.1:
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++) { ^~~~~~~
GCC basically fails to see, that the virDomainChrGetDomainPtrsInternal will never actually return NULL because it's never called over a domain char device with _LAST type. But to make it shut up, lets turn this function into returning an integer and check in the callers if a zero value value was returned.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/conf/domain_conf.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 568c699..2efe0a3 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -14038,7 +14038,7 @@ virDomainChrFind(virDomainDefPtr def,
/* Return the address within vmdef to be modified when working with a * chrdefptr of the given type. */ -static void +static int ATTRIBUTE_RETURN_CHECK virDomainChrGetDomainPtrsInternal(virDomainDefPtr vmdef, virDomainChrDeviceType type, virDomainChrDefPtr ***arrPtr, @@ -14070,6 +14070,8 @@ virDomainChrGetDomainPtrsInternal(virDomainDefPtr vmdef, *cntPtr = NULL; break; } + + return (*arrPtr && *cntPtr) ? 0 : -1;
This doesn't set any error. The VIR_DOMAIN_CHR_DEVICE_TYPE_LAST case should do so and possibly return -1 right away to avoid the ternary.
}
[...]
@@ -14104,7 +14105,9 @@ virDomainChrPreAlloc(virDomainDefPtr vmdef, virDomainChrDefPtr **arrPtr = NULL; size_t *cntPtr = NULL;
- virDomainChrGetDomainPtrsInternal(vmdef, chr->deviceType, &arrPtr, &cntPtr); + if (virDomainChrGetDomainPtrsInternal(vmdef, chr->deviceType, + &arrPtr, &cntPtr) < 0) + return -1;
So this will report the "unknown error".
return VIR_REALLOC_N(*arrPtr, *cntPtr + 1);
ACK with the error added.