In a few places it may happen that the array we want to sort is still NULL (e.g. because there were no leases found, no paths for secdriver to lock or no cache banks). However, passing NULL to qsort() is undefined and even though glibc plays nicely we shouldn't rely on undefined behaviour. Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- src/conf/capabilities.c | 6 ++++-- src/security/security_manager.c | 3 ++- tools/nss/libvirt_nss.c | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c index 2f9a1e7d1f..d3b22f7dd0 100644 --- a/src/conf/capabilities.c +++ b/src/conf/capabilities.c @@ -1924,8 +1924,10 @@ virCapabilitiesInitCaches(virCaps *caps) /* Sort the array in order for the tests to be predictable. This way we can * still traverse the directory instead of guessing names (in case there is * 'index1' and 'index3' but no 'index2'). */ - qsort(caps->host.cache.banks, caps->host.cache.nbanks, - sizeof(*caps->host.cache.banks), virCapsHostCacheBankSorter); + if (caps->host.cache.banks) { + qsort(caps->host.cache.banks, caps->host.cache.nbanks, + sizeof(*caps->host.cache.banks), virCapsHostCacheBankSorter); + } if (virCapabilitiesInitResctrlMemory(caps) < 0) goto cleanup; diff --git a/src/security/security_manager.c b/src/security/security_manager.c index d8b84e2861..0af581bc8b 100644 --- a/src/security/security_manager.c +++ b/src/security/security_manager.c @@ -1355,7 +1355,8 @@ virSecurityManagerMetadataLock(virSecurityManager *mgr G_GNUC_UNUSED, * paths in the same order and thus no deadlock can occur. * Lastly, it makes searching for duplicate paths below * simpler. */ - qsort(paths, npaths, sizeof(*paths), cmpstringp); + if (paths) + qsort(paths, npaths, sizeof(*paths), cmpstringp); for (i = 0; i < npaths; i++) { const char *p = paths[i]; diff --git a/tools/nss/libvirt_nss.c b/tools/nss/libvirt_nss.c index b021efc3c9..a6e8e4b12b 100644 --- a/tools/nss/libvirt_nss.c +++ b/tools/nss/libvirt_nss.c @@ -69,7 +69,8 @@ static void sortAddr(leaseAddress *tmpAddress, size_t ntmpAddress) { - qsort(tmpAddress, ntmpAddress, sizeof(*tmpAddress), leaseAddressSorter); + if (tmpAddress) + qsort(tmpAddress, ntmpAddress, sizeof(*tmpAddress), leaseAddressSorter); } -- 2.31.1