Don't call qsort() over NULL

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@redhat.com>
Reviewed-by: Tim Wiederhake <twiederh@redhat.com>
This commit is contained in:
Michal Privoznik 2021-06-14 12:46:02 +02:00
parent 8f52136592
commit 1ab5a37c4a
3 changed files with 8 additions and 4 deletions

View File

@ -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;

View File

@ -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];

View File

@ -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);
}