]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh-snapshot: Avoid passing NULL to qsort() in virshSnapshotListCollect()
authorMichal Privoznik <mprivozn@redhat.com>
Thu, 7 Sep 2023 08:56:01 +0000 (10:56 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 7 Sep 2023 09:37:54 +0000 (11:37 +0200)
If a domain has no snapshots and 'virsh snapshot-list' is called,
this gets all the way down to virshSnapshotListCollect() which
then collects all snapshots (none), and passes them to qsort()
which doesn't like being called with NULL:

  extern void qsort (void *__base, size_t __nmemb, size_t __size,
                     __compar_fn_t __compar) __nonnull ((1, 4));

Resolves: https://gitlab.com/libvirt/libvirt/-/issues/533
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
tools/virsh-snapshot.c

index d7889a38e4780c208506d3804fc0e6c81e360146..c85258c09ae15c273f5f5b5eac3b28a6fe1a70ad 100644 (file)
@@ -1310,9 +1310,11 @@ virshSnapshotListCollect(vshControl *ctl, virDomainPtr dom,
             }
         }
     }
-    if (!(orig_flags & VIR_DOMAIN_SNAPSHOT_LIST_TOPOLOGICAL))
+    if (!(orig_flags & VIR_DOMAIN_SNAPSHOT_LIST_TOPOLOGICAL) &&
+        snaplist->snaps && snaplist->nsnaps) {
         qsort(snaplist->snaps, snaplist->nsnaps, sizeof(*snaplist->snaps),
               virshSnapSorter);
+    }
     snaplist->nsnaps -= deleted;
 
     ret = g_steal_pointer(&snaplist);