Received: from eggs.gnu.org ([2001:4830:134:3::10]:57373) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bWaWr-0006WN-79 for qemu-devel@nongnu.org; Sun, 07 Aug 2016 22:43:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bWaWn-0004UG-0U for qemu-devel@nongnu.org; Sun, 07 Aug 2016 22:43:01 -0400 From: David Gibson Date: Mon, 8 Aug 2016 12:44:42 +1000 Message-Id: <1470624282-5559-4-git-send-email-david@gibson.dropbear.id.au> In-Reply-To: <1470624282-5559-1-git-send-email-david@gibson.dropbear.id.au> References: <1470624282-5559-1-git-send-email-david@gibson.dropbear.id.au> Subject: [Qemu-devel] [PULL 3/3] spapr: Fix undefined behaviour in spapr_tce_reset() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: peter.maydell@linaro.org Cc: agraf@suse.de, qemu-ppc@nongnu.org, qemu-devel@nongnu.org, bharata@linux.vnet.ibm.com, imammedo@redhat.com, David Gibson 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 Signed-off-by: David Gibson --- hw/ppc/spapr_iommu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hw/ppc/spapr_iommu.c b/hw/ppc/spapr_iommu.c index d57b05d..6bc4d4d 100644 --- a/hw/ppc/spapr_iommu.c +++ b/hw/ppc/spapr_iommu.c @@ -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, -- 2.7.4