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

Yi Wang posted 1 patch 2 years, 9 months ago
Failed in applying to current master (apply log)
src/qemu/qemu_driver.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
[PATCH] qemu: Fix error returned value in qemuGetProcessInfo when fscanf execute failed
Posted by Yi Wang 2 years, 9 months ago
From: Long YunJian <long.yunjian@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@zte.com.cn>
Signed-off-by: Yi Wang <wang.yi59@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
Re: [PATCH] qemu: Fix error returned value in qemuGetProcessInfo when fscanf execute failed
Posted by Michal Prívozník 2 years, 9 months ago
On 7/15/21 8:18 AM, Yi Wang wrote:
> From: Long YunJian <long.yunjian@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@zte.com.cn>
> Signed-off-by: Yi Wang <wang.yi59@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;

There's another VIR_FORCE_FCLOSE() at the end of the function. It should
be removed.

But is there an actual error you're seeing? Has /proc/NNN/stat format
changed? Also, please use git send-email if possible - it allows
reviewers to use git am (which doesn't work with multipart e-mails).

Michal

Re:[PATCH] qemu: Fix error returned value in qemuGetProcessInfo when fscanf execute failed
Posted by wang.yi59@zte.com.cn 2 years, 9 months ago
Hi Michal,

Thanks for your reply.

> On 7/15/21 8:18 AM, Yi Wang wrote:
> > From: Long YunJian <long.yunjian@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.
> >
...
> >                 /* 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;
>
> There's another VIR_FORCE_FCLOSE() at the end of the function. It should
> be removed.

VIR_FORCE_FCLOSE() at the end of the function means in the
situation of success, but VIR_FORCE_FCLOSE before return -1 is
failure situation. if removed, it will result in not close pidinfo.

>
> But is there an actual error you're seeing? Has /proc/NNN/stat format
> changed? Also, please use git send-email if possible - it allows
> reviewers to use git am (which doesn't work with multipart e-mails).

I haven't meet fscanf return error,  but in some abnormal situation,
such as interrupted by a signal or out of memory, it may return failure.

Actually I used git send-email to send this patch, if there's something
wrong, please let me know and I will forward that to our IT department :)

>
> Michal