]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Don't call qsort() over NULL
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 14 Jun 2021 10:46:02 +0000 (12:46 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 14 Jun 2021 12:16:44 +0000 (14:16 +0200)
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>
src/conf/capabilities.c
src/security/security_manager.c
tools/nss/libvirt_nss.c

index 2f9a1e7d1fbf03bd68dbacab58e3e653ec9977f6..d3b22f7dd0bb9532f073d07975cecf7007f01114 100644 (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;
index d8b84e28618dfd8d26b12c9a5459f3871731075b..0af581bc8b56026f295422103a1cad125522a374 100644 (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];
index b021efc3c9b037aaa7414b94828d68896f6744c1..a6e8e4b12b48222d0e426338b0b554667e77d502 100644 (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);
 }