Skip to contentSkip to conversation

Add missing initialization for g_autofree variables

PATCH Superseded [email protected] 2 participants 3 messages Mar 15, 2021

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 to NULL for these declaration to prevent this warning.

---
* From v1:
-- 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 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/s390x/s390-pci-vfio.c +2 -2
index ead4f222d5..0ee7dc21f2 100644
@@ -29,7 +29,7 @@
  */
 bool s390_pci_update_dma_avail(int fd, unsigned int *avail)
 {
-    g_autofree struct vfio_iommu_type1_info *info;
+    g_autofree struct vfio_iommu_type1_info *info = NULL;
     uint32_t argsz;
 
     assert(avail);
@@ -230,7 +230,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 v2 1/1
#1 lore raw
Quoted 31 lines · On 15/03/2021 09.00, [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 to NULL for these declaration to prevent this warning.

---
* From v1:
-- 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 | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/s390x/s390-pci-vfio.c +0 -0
index ead4f222d5..0ee7dc21f2 100644

hw/s390x/s390-pci-vfio.c line 35

Quoted 8 lines
hw/s390x/s390-pci-vfio.c +1 -1
@@ -29,7 +29,7 @@
   */
  bool s390_pci_update_dma_avail(int fd, unsigned int *avail)
  {
-    g_autofree struct vfio_iommu_type1_info *info;
+    g_autofree struct vfio_iommu_type1_info *info = NULL;
      uint32_t argsz;
 
      assert(avail);

I'd maybe rather rework the functions like this:

hw/s390x/s390-pci-vfio.c +2 -5
index ead4f222d5..1fe71fd93f 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 236

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

Anyway,

#2 lore raw
Quoted 48 lines · Thomas Huth
Quoted 31 lines · On 15/03/2021 09.00, [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 to NULL for these declaration to prevent this warning.

---
* From v1:
-- 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 | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/s390x/s390-pci-vfio.c +0 -0
index ead4f222d5..0ee7dc21f2 100644

hw/s390x/s390-pci-vfio.c line 34

Quoted 7 lines
hw/s390x/s390-pci-vfio.c +1 -1
@@ -29,7 +29,7 @@
   */
  bool s390_pci_update_dma_avail(int fd, unsigned int *avail)
  {
-    g_autofree struct vfio_iommu_type1_info *info;
+    g_autofree struct vfio_iommu_type1_info *info = NULL;
      uint32_t argsz;
      assert(avail);

I'd maybe rather rework the functions like this:

hw/s390x/s390-pci-vfio.c +0 -0
index ead4f222d5..1fe71fd93f 100644
@@ -29,14 +29,11 @@
  */

hw/s390x/s390-pci-vfio.c line 37

Quoted 10 lines
hw/s390x/s390-pci-vfio.c +2 -5
 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);
-

Hi Thomas,

I thought about it but for some reason I miss-read the code and though that avail is used for calculating argsz and didn't want to use it before assert.

I'll send new version with this change.

Quoted 19 lines
hw/s390x/s390-pci-vfio.c +0 -0
     /*
      * 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 +230,7 @@ static void s390_pci_read_pfip(S390PCIBusDevice *pbdev,

hw/s390x/s390-pci-vfio.c

Quoted 9 lines
hw/s390x/s390-pci-vfio.c +0 -0
   */
  void s390_pci_get_clp_info(S390PCIBusDevice *pbdev)
  {
hw/s390x/s390-pci-vfio.c +1 -1
-    g_autofree struct vfio_device_info *info;
+    g_autofree struct vfio_device_info *info = NULL;
      VFIOPCIDevice *vfio_pci;
      uint32_t argsz;
      int fd;

Anyway,