Skip to contentSkip to conversation

Add missing initialization for g_autofree variables

PATCH Applied [email protected] 5 participants 6 messages Mar 15, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Add missing initialization for g_autofree variables

From: Miroslav Rezanina <[email protected]>

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.

---
* 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(-)
hw/s390x/s390-pci-vfio.c +3 -6
index ead4f222d5..2a153fa8c9 100644
@@ -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;
Signature · 1 line
2.27.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 3 lines · On 3/15/21 11:13 AM, [email protected] wrote:

From: Miroslav Rezanina <[email protected]>

When declaring g_autofree variable without inicialization, compiler

Typo "initialization"

Quoted 10 lines

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

"initialization"

Quoted 8 lines

comply with coding style.

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

Quoted 7 lines

* 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(-)

Maybe "hw/s390x/s390-pci-vfio" prefix in subject?

Otherwise:

#2 lore raw
In reply to [email protected]
Quoted 6 lines · On Mon, 15 Mar 2021 11:13:52 +0100 [email protected] wrote:

From: Miroslav Rezanina <[email protected]>

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

Quoted 8 lines

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.

Fixes cd7498d07fbb ("s390x/pci: Add routine to get the vfio dma available count")
Fixes 1e7552ff5c34 ("s390x/pci: get zPCI function info from host")

I can fix the spelling mistakes pointed out by Phil while applying, and also add an 's390x/pci' prefix.

Would not mind a quick test run from someone with the hardware.

Quoted 45 lines

---
* 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(-)
hw/s390x/s390-pci-vfio.c +3 -6
index ead4f222d5..2a153fa8c9 100644
@@ -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;
Tested-by
#3 lore raw
Quoted 18 lines · Cornelia Huck
Quoted 6 lines · On Mon, 15 Mar 2021 11:13:52 +0100 [email protected] wrote:

From: Miroslav Rezanina <[email protected]>

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

Quoted 8 lines

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.

Quoted 7 lines
Fixes cd7498d07fbb ("s390x/pci: Add routine to get the vfio dma available count")
Fixes 1e7552ff5c34 ("s390x/pci: get zPCI function info from host")

I can fix the spelling mistakes pointed out by Phil while applying, and also add an 's390x/pci' prefix.

Would not mind a quick test run from someone with the hardware.

Sure, I took this for a spin (ConnectX-3 VF via vfio-pci) and made sure both code paths were being driven without issue. Code looks fine as well.

Quoted 47 lines
Quoted 46 lines

---
* 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(-)
hw/s390x/s390-pci-vfio.c +3 -6
index ead4f222d5..2a153fa8c9 100644
@@ -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 on v3 1/1
#4 lore raw
In reply to [email protected]
Quoted 52 lines · On 15/03/2021 11.13, [email protected] wrote:

From: Miroslav Rezanina <[email protected]>

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.

---
* 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(-)
hw/s390x/s390-pci-vfio.c +2 -5
index ead4f222d5..2a153fa8c9 100644
@@ -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

hw/s390x/s390-pci-vfio.c static void s390_pci_read_pfip(S390PCIBusDevice *pbdev, · line 233

Quoted 8 lines
hw/s390x/s390-pci-vfio.c +1 -1
@@ -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;
#5 lore raw
In reply to [email protected]
Quoted 51 lines · On Mon, 15 Mar 2021 11:13:52 +0100 [email protected] wrote:

From: Miroslav Rezanina <[email protected]>

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.

---
* 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(-)
hw/s390x/s390-pci-vfio.c +2 -5
index ead4f222d5..2a153fa8c9 100644
@@ -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

hw/s390x/s390-pci-vfio.c static void s390_pci_read_pfip(S390PCIBusDevice *pbdev, · line 233

Quoted 8 lines
hw/s390x/s390-pci-vfio.c +1 -1
@@ -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;

Thanks, applied.