[PATCH v2] qemuDomainGetGuestInfo: Exit early if getting info fails

Michal Privoznik posted 1 patch 3 years, 4 months ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/75477002e3d18ec776e90c0e55d4f20728fa5ab3.1606840858.git.mprivozn@redhat.com
src/qemu/qemu_driver.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
[PATCH v2] qemuDomainGetGuestInfo: Exit early if getting info fails
Posted by Michal Privoznik 3 years, 4 months ago
If there is an error getting info from guest agent, then the
control on qemuDomainGetGuestInfo() jumps onto 'exitagent' label
and subsequently continues on 'endagentjob'. Both labels are hit
also in success case too. The control then continues by
attempting to match fetched info (e.g. disk addresses) with
domain def. But this is needless - the API will return error
regardless.

To return early from the function move both 'exitagent' and
'endagentjob' labels at the end of the function and jump straight
onto 'cleanup' afterwards. This allows us to set 'ret = 0' later
- only when we know we succeeded.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---

v2 of:

https://www.redhat.com/archives/libvir-list/2020-December/msg00020.html

 src/qemu/qemu_driver.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 8eaa3ce68f..bca1c84630 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -20116,12 +20116,7 @@ qemuDomainGetGuestInfo(virDomainPtr dom,
         }
     }
 
-    ret = 0;
-
- exitagent:
     qemuDomainObjExitAgent(vm, agent);
-
- endagentjob:
     qemuDomainObjEndAgentJob(vm);
 
     if (nfs > 0 || ndisks > 0) {
@@ -20143,6 +20138,8 @@ qemuDomainGetGuestInfo(virDomainPtr dom,
         qemuDomainObjEndJob(driver, vm);
     }
 
+    ret = 0;
+
  cleanup:
     for (i = 0; i < nfs; i++)
         qemuAgentFSInfoFree(agentfsinfo[i]);
@@ -20153,6 +20150,13 @@ qemuDomainGetGuestInfo(virDomainPtr dom,
 
     virDomainObjEndAPI(&vm);
     return ret;
+
+ exitagent:
+    qemuDomainObjExitAgent(vm, agent);
+
+ endagentjob:
+    qemuDomainObjEndAgentJob(vm);
+    goto cleanup;
 }
 
 
-- 
2.26.2

Re: [PATCH v2] qemuDomainGetGuestInfo: Exit early if getting info fails
Posted by Ján Tomko 3 years, 4 months ago
On a Tuesday in 2020, Michal Privoznik wrote:
>If there is an error getting info from guest agent, then the
>control on qemuDomainGetGuestInfo() jumps onto 'exitagent' label
>and subsequently continues on 'endagentjob'. Both labels are hit
>also in success case too. The control then continues by
>attempting to match fetched info (e.g. disk addresses) with
>domain def. But this is needless - the API will return error
>regardless.
>
>To return early from the function move both 'exitagent' and
>'endagentjob' labels at the end of the function and jump straight
>onto 'cleanup' afterwards. This allows us to set 'ret = 0' later
>- only when we know we succeeded.
>
>Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
>---
>
>v2 of:
>
>https://www.redhat.com/archives/libvir-list/2020-December/msg00020.html
>
> src/qemu/qemu_driver.c | 14 +++++++++-----
> 1 file changed, 9 insertions(+), 5 deletions(-)
>

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano