[PATCH] qemu: Fix error returned value in qemuGetProcessInfo when fscanf execute failed

Yi Wang wang.yi59 at zte.com.cn
Thu Jul 15 06:18:12 UTC 2021


From: Long YunJian <long.yunjian at zte.com.cn>

If fscanf execute failed, qemuGetProcessInfo shuld return -1,
but it return 0 at the end. Zero means success for the caller,
so we shuld return -1 in the case of failure.

Signed-off-by: Long YunJian <long.yunjian at zte.com.cn>
Signed-off-by: Yi Wang <wang.yi59 at zte.com.cn>
---
 src/qemu/qemu_driver.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index df44c3fbd0..4c3785fa36 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -1442,11 +1442,13 @@ qemuGetProcessInfo(unsigned long long *cpuTime, int *lastCpu, long *vm_rss,
         return -1;
 
     pidinfo = fopen(proc, "r");
+    if (!pidinfo) {
+        return -1;
+    }
 
     /* See 'man proc' for information about what all these fields are. We're
      * only interested in a very few of them */
-    if (!pidinfo ||
-        fscanf(pidinfo,
+    if (fscanf(pidinfo,
                /* pid -> stime */
                "%*d (%*[^)]) %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %llu %llu"
                /* cutime -> endcode */
@@ -1455,6 +1457,8 @@ qemuGetProcessInfo(unsigned long long *cpuTime, int *lastCpu, long *vm_rss,
                "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*d %d",
                &usertime, &systime, &rss, &cpu) != 4) {
         VIR_WARN("cannot parse process status data");
+        VIR_FORCE_FCLOSE(pidinfo);
+        return -1;
     }
 
     /* We got jiffies
-- 
2.18.1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20210715/9e51264c/attachment-0001.htm>


More information about the libvir-list mailing list