qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v3] Add missing initialization for g_autofree variables


From: Thomas Huth
Subject: Re: [PATCH v3] Add missing initialization for g_autofree variables
Date: Mon, 15 Mar 2021 12:34:31 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0

On 15/03/2021 11.13, mrezanin@redhat.com wrote:
From: Miroslav Rezanina <mrezanin@redhat.com>

When declaring g_autofree variable without inicialization, compiler
will raise "may be used uninitialized in this function" warning due
to automatic free handling.

This is mentioned in docs/devel/style.rst (quote from section
"Automatic memory deallocation"):

   * Variables declared with g_auto* MUST always be initialized,
     otherwise the cleanup function will use uninitialized stack memory

Add inicialization for these declarations to prevent the warning and
comply with coding style.

Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>

---
* v3:
   -- allocate in s390_pci_update_dma_avail instead of NULL init

* v2:
   -- Removed fixes in hw/remote/memory.c and hw/remote/proxy.c
      fixed by patch sent by Zenghui Yu (multi-process: Initialize
      variables declared with g_auto*)
---
  hw/s390x/s390-pci-vfio.c | 9 +++------
  1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/hw/s390x/s390-pci-vfio.c b/hw/s390x/s390-pci-vfio.c
index ead4f222d5..2a153fa8c9 100644
--- a/hw/s390x/s390-pci-vfio.c
+++ b/hw/s390x/s390-pci-vfio.c
@@ -29,14 +29,11 @@
   */
  bool s390_pci_update_dma_avail(int fd, unsigned int *avail)
  {
-    g_autofree struct vfio_iommu_type1_info *info;
-    uint32_t argsz;
+    uint32_t argsz = sizeof(struct vfio_iommu_type1_info);
+    g_autofree struct vfio_iommu_type1_info *info = g_malloc0(argsz);
assert(avail); - argsz = sizeof(struct vfio_iommu_type1_info);
-    info = g_malloc0(argsz);
-
      /*
       * If the specified argsz is not large enough to contain all capabilities
       * it will be updated upon return from the ioctl.  Retry until we have
@@ -230,7 +227,7 @@ static void s390_pci_read_pfip(S390PCIBusDevice *pbdev,
   */
  void s390_pci_get_clp_info(S390PCIBusDevice *pbdev)
  {
-    g_autofree struct vfio_device_info *info;
+    g_autofree struct vfio_device_info *info = NULL;
      VFIOPCIDevice *vfio_pci;
      uint32_t argsz;
      int fd;


Reviewed-by: Thomas Huth <thuth@redhat.com>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]