From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52557) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNLwM-0002QC-DG for qemu-devel@nongnu.org; Tue, 11 Mar 2014 08:37:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WNLwH-0000Vk-2H for qemu-devel@nongnu.org; Tue, 11 Mar 2014 08:37:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:31164) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNLwG-0000Vg-QB for qemu-devel@nongnu.org; Tue, 11 Mar 2014 08:37:45 -0400 Date: Tue, 11 Mar 2014 14:31:39 +0200 From: "Michael S. Tsirkin" Message-ID: <20140311123139.GA26907@redhat.com> References: <1394481363-5333-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Subject: Re: [Qemu-devel] [PATCH v2] acpi-build: don't access unaligned addresses List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Maydell Cc: QEMU Developers , Anthony Liguori On Tue, Mar 11, 2014 at 12:29:05PM +0000, Peter Maydell wrote: > On 10 March 2014 19:56, Michael S. Tsirkin wrote: > > casting an unaligned address to e.g. > > uint32_t can trigger undefined behaviour in C. > > Replace cast + assignment with memcpy. > > > > Reported-by: Peter Maydell > > Signed-off-by: Michael S. Tsirkin > > This does fix the clang warnings. > > > -/* Get pointer within table in a safe manner */ > > -#define ACPI_BUILD_PTR(table, size, off, type) \ > > - ((type *)(acpi_data_get_ptr(table, size, off, sizeof(type)))) > > +/* Set a value within table in a safe manner */ > > +#define ACPI_BUILD_SET_LE(table, size, off, bits, val) \ > > + do { \ > > + uint64_t ACPI_BUILD_SET_LE_val = cpu_to_le64(val); \ > > + memcpy(acpi_data_get_ptr(table, size, off, \ > > + (bits) / BITS_PER_BYTE), \ > > + &ACPI_BUILD_SET_LE_val, \ > > + (bits) / BITS_PER_BYTE); \ > > + } while (0) > > Personally I would have done: > > #define acpi_stb(table, size, off, val) \ > stb_le_p(acpi_data_get_ptr(table, size, off, 1), val) > #define acpi_stw(table, size, off, val) \ > stw_le_p(acpi_data_get_ptr(table, size, off, 2), val) > #define acpi_stl(table, size, off, val) \ > stl_le_p(acpi_data_get_ptr(table, size, off, 4), val) > #define acpi_stq(table, size, off, val) \ > stq_le_p(acpi_data_get_ptr(table, size, off, 8), val) > > which keeps the grubby details of memcpy and byteswapping > in bswap.h. However since it's purely inside this file and > not specifying an API to the rest of QEMU I don't object > if you prefer the approach you've taken. > > thanks > -- PMM I'll think about it some more - this can be a patch on top. Let's fix the bug for now.