spapr: Fix undefined behaviour in spapr_tce_reset()
authorDavid Gibson <david@gibson.dropbear.id.au>
Mon, 8 Aug 2016 00:06:25 +0000 (10:06 +1000)
committerDavid Gibson <david@gibson.dropbear.id.au>
Mon, 8 Aug 2016 00:06:25 +0000 (10:06 +1000)
When a TCE table (sPAPR IOMMU context) is in disabled state (which is true
by default for the 64-bit window), it has tcet->nb_table == 0 and
tcet->table == NULL.  However, on system reset, spapr_tce_reset() executes,
which unconditionally calls
        memset(tcet->table, 0, table_size);

We get away with this in practice, because it's a zero length memset(),
but memset() on a NULL pointer is undefined behaviour, so we should not
call it in this case.

Reported-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
hw/ppc/spapr_iommu.c

index d57b05d5c0f12c7d85b056984565931efa6d339e..6bc4d4db33637e10111b41b41ad3c1e966e3f2fd 100644 (file)
@@ -385,7 +385,9 @@ static void spapr_tce_reset(DeviceState *dev)
     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(dev);
     size_t table_size = tcet->nb_table * sizeof(uint64_t);
 
-    memset(tcet->table, 0, table_size);
+    if (tcet->nb_table) {
+        memset(tcet->table, 0, table_size);
+    }
 }
 
 static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,