]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
libvirt-guests.sh: Declare and assign separately to avoid masking return values
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 19 Jan 2026 10:28:47 +0000 (11:28 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 19 Jan 2026 13:21:57 +0000 (14:21 +0100)
In Bash, the following code does not do what you think it does:

  func() {
      local var=$(false)
      echo $?
  }

Here, '0' is echoed even though false is designed to exit with a
non-zero code. This is because in fact the last executed command
is 'local' not 'false' and thus '$?' contains zero as in "yeah,
the variable is successfully declared" [1]. In our libvirt-guest
shell script, there are a few places like this. Fix them.

1: bash_builtins(1)
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tools/libvirt-guests.sh.in

index f2db1282adbd1c36002f4f744815d9f894050ddf..e05bfdba61ea9edbc714f5540551e0bfe9eb19b2 100644 (file)
@@ -106,8 +106,9 @@ test_connect()
 list_guests() {
     local uri="$1"
     local persistent="$2"
-    local list="$(run_virsh_c "$uri" list --uuid $persistent)"
+    local list
 
+    list="$(run_virsh_c "$uri" list --uuid $persistent)"
     if [ $? -ne 0 ]; then
         RETVAL=1
         return 1
@@ -482,7 +483,8 @@ stop() {
 
         eval_gettext "Running guests on \$uri URI: "
 
-        local list="$(list_guests "$uri")"
+        local list
+        list="$(list_guests "$uri")"
         if [ $? -eq 0 ]; then
             local empty=true
             for uuid in $list; do
@@ -498,7 +500,8 @@ stop() {
         fi
 
         if "$persistent_only"; then
-            local transient="$(list_guests "$uri" "--transient")"
+            local transient
+            transient="$(list_guests "$uri" "--transient")"
             if [ $? -eq 0 ]; then
                 local empty="true"
                 local uuid=