Skip to contentSkip to conversation

hw/pci-bridge/cxl_upstream.c: Use g_new0() in build_cdat_table()

PATCH In review Peter Maydell 3 participants 3 messages Jul 18, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/pci-bridge/cxl_upstream.c: Use g_new0() in build_cdat_table()

In build_cdat_table() we do:
*cdat_table = g_malloc0(sizeof(*cdat_table) * CXL_USP_CDAT_NUM_ENTRIES);
This is wrong because:
- cdat_table has type CDATSubHeader ***
- so *cdat_table has type CDATSubHeader **
- so the array we're allocating here should be items of type CDATSubHeader *
- but we pass sizeof(*cdat_table), which is sizeof(CDATSubHeader **), implying that we're allocating an array of CDATSubHeader **

It happens that sizeof(CDATSubHeader **) == sizeof(CDATSubHeader *) so nothing blows up, but this should be sizeof(**cdat_table).

Avoid this excessively hard-to-understand code by using g_new0() instead, which will do the type checking for us. While we're here, we can drop the useless check against failure, as g_malloc0() and g_new0() never fail.

This fixes Coverity issue CID 1508120.

---
Disclaimer: I have not tested this beyond any testing you get from 'make check' and 'make check-avocado'.

---
 hw/pci-bridge/cxl_upstream.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
hw/pci-bridge/cxl_upstream.c +1 -4
index ef47e5d6253..9159f48a8cb 100644
@@ -274,10 +274,7 @@ static int build_cdat_table(CDATSubHeader ***cdat_table, void *priv)
         };
     }
 
-    *cdat_table = g_malloc0(sizeof(*cdat_table) * CXL_USP_CDAT_NUM_ENTRIES);
-    if (!*cdat_table) {
-        return -ENOMEM;
-    }
+    *cdat_table = g_new0(CDATSubHeader *, CXL_USP_CDAT_NUM_ENTRIES);
 
     /* Header always at start of structure */
     (*cdat_table)[CXL_USP_CDAT_SSLBIS_LAT] = g_steal_pointer(&sslbis_latency);
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Peter Maydell

In build_cdat_table() we do:
*cdat_table = g_malloc0(sizeof(*cdat_table) * CXL_USP_CDAT_NUM_ENTRIES);
This is wrong because:
- cdat_table has type CDATSubHeader ***

Yes

Quoted 1 line

- so *cdat_table has type CDATSubHeader **

Yes

Quoted 1 line

- so the array we're allocating here should be items of type CDATSubHeader *

Yes

Quoted 1 line

- but we pass sizeof(*cdat_table), which is sizeof(CDATSubHeader **),

Indeed

Quoted 1 line

implying that we're allocating an array of CDATSubHeader **

Ouch

Quoted 1 line

It happens that sizeof(CDATSubHeader **) == sizeof(CDATSubHeader *)

Ah!

Quoted 1 line

so nothing blows up, but this should be sizeof(**cdat_table).

Still, what a mess :)

Quoted 4 lines

Avoid this excessively hard-to-understand code by using g_new0() instead, which will do the type checking for us. While we're here, we can drop the useless check against failure, as g_malloc0() and g_new0() never fail.

Quoted 9 lines

This fixes Coverity issue CID 1508120.

---
Disclaimer: I have not tested this beyond any testing you get from 'make check' and 'make check-avocado'.

---
  hw/pci-bridge/cxl_upstream.c | 5 +----
  1 file changed, 1 insertion(+), 4 deletions(-)
Reviewed-by Jonathan Cameron
#2 lore raw
Quoted 34 lines · On Tue, 18 Jul 2023 14:10:24 +0200 Philippe Mathieu-Daudé <[email protected]>…
Quoted 4 lines · Peter Maydell

In build_cdat_table() we do:
*cdat_table = g_malloc0(sizeof(*cdat_table) * CXL_USP_CDAT_NUM_ENTRIES);
This is wrong because:
- cdat_table has type CDATSubHeader ***

Yes

Quoted 1 line

- so *cdat_table has type CDATSubHeader **

Yes

Quoted 1 line

- so the array we're allocating here should be items of type CDATSubHeader *

Yes

Quoted 1 line

- but we pass sizeof(*cdat_table), which is sizeof(CDATSubHeader **),

Indeed

Quoted 1 line

implying that we're allocating an array of CDATSubHeader **

Ouch

Quoted 1 line

It happens that sizeof(CDATSubHeader **) == sizeof(CDATSubHeader *)

Ah!

Quoted 1 line

so nothing blows up, but this should be sizeof(**cdat_table).

Still, what a mess :)

Quoted 13 lines
Quoted 4 lines

Avoid this excessively hard-to-understand code by using g_new0() instead, which will do the type checking for us. While we're here, we can drop the useless check against failure, as g_malloc0() and g_new0() never fail.

Quoted 6 lines

This fixes Coverity issue CID 1508120.

---
Disclaimer: I have not tested this beyond any testing you get from 'make check' and 'make check-avocado'.

Ah. Had this on my todo list form your report but got distracted. Anyhow, thanks for fixing it up.

Tested somewhat. Actually poking this particular DOE instance to read the CDAT requires a bunch of kernel hacks I haven't forward ported recently and I don't want to delay this until I get back to them - depends on some core PCI subsystem work that is ongoing. Code isn't dependent on reading though so I can't see how this would break anything.

Quoted 4 lines
Quoted 3 lines
---
  hw/pci-bridge/cxl_upstream.c | 5 +----
  1 file changed, 1 insertion(+), 4 deletions(-)