[PATCH 1/4] virpci: Avoid Clang false positive

Michal Privoznik posted 4 patches 4 years, 5 months ago
[PATCH 1/4] virpci: Avoid Clang false positive
Posted by Michal Privoznik 4 years, 5 months ago
The virPCIDeviceIsBehindSwitchLackingACS() function checks
whether given PCI device is not behind a switch that lacks ACS.
It does so by starting at given device and traversing up, one
parent at time towards the root. The parent device is obtained
via virPCIDeviceGetParent() which allocates new virPCIDevice
structure. For freeing the structure we use g_autoptr() and a
temporary variable @tmp. However, Clang fails to understand our
clever algorithm and complains that the variable is set but never
used. This is obviously a false positive, but using a small trick
we can shut Clang up.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/util/virpci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/util/virpci.c b/src/util/virpci.c
index 915a4903ca..f307580a53 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -2150,8 +2150,8 @@ virPCIDeviceIsBehindSwitchLackingACS(virPCIDevice *dev)
                 return 1;
         }
 
-        tmp = parent;
-        ret = virPCIDeviceGetParent(parent, &parent);
+        tmp = g_steal_pointer(&parent);
+        ret = virPCIDeviceGetParent(tmp, &parent);
         if (ret < 0)
             return -1;
     } while (parent);
-- 
2.31.1

Re: [PATCH 1/4] virpci: Avoid Clang false positive
Posted by Peter Krempa 4 years, 5 months ago
Please pick a better summary.

Something along:

virpci: Clarify lifetime of temporary object


On Wed, Aug 25, 2021 at 14:54:56 +0200, Michal Privoznik wrote:
> The virPCIDeviceIsBehindSwitchLackingACS() function checks
> whether given PCI device is not behind a switch that lacks ACS.
> It does so by starting at given device and traversing up, one
> parent at time towards the root. The parent device is obtained
> via virPCIDeviceGetParent() which allocates new virPCIDevice
> structure. For freeing the structure we use g_autoptr() and a
> temporary variable @tmp. However, Clang fails to understand our
> clever algorithm and complains that the variable is set but never
> used. This is obviously a false positive, but using a small trick
> we can shut Clang up.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/util/virpci.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/src/util/virpci.c b/src/util/virpci.c
> index 915a4903ca..f307580a53 100644
> --- a/src/util/virpci.c
> +++ b/src/util/virpci.c
> @@ -2150,8 +2150,8 @@ virPCIDeviceIsBehindSwitchLackingACS(virPCIDevice *dev)
>                  return 1;
>          }
>  
> -        tmp = parent;
> -        ret = virPCIDeviceGetParent(parent, &parent);
> +        tmp = g_steal_pointer(&parent);
> +        ret = virPCIDeviceGetParent(tmp, &parent);
>          if (ret < 0)
>              return -1;
>      } while (parent);

Reviewed-by: Peter Krempa <pkrempa@redhat.com>