From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:59932) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TtI7r-0004re-Kg for qemu-devel@nongnu.org; Thu, 10 Jan 2013 08:24:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TtI7p-0008So-5V for qemu-devel@nongnu.org; Thu, 10 Jan 2013 08:24:55 -0500 Received: from oxygen.pond.sub.org ([2a01:4f8:121:10e4::3]:47335) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TtI7o-0008SI-Uz for qemu-devel@nongnu.org; Thu, 10 Jan 2013 08:24:53 -0500 Received: from blackfin.pond.sub.org (p5B32A660.dip.t-dialin.net [91.50.166.96]) by oxygen.pond.sub.org (Postfix) with ESMTPA id 08CB39FE68 for ; Thu, 10 Jan 2013 14:24:51 +0100 (CET) From: Markus Armbruster Date: Thu, 10 Jan 2013 14:24:49 +0100 Message-Id: <1357824290-31222-2-git-send-email-armbru@redhat.com> In-Reply-To: <1357824290-31222-1-git-send-email-armbru@redhat.com> References: <1357824290-31222-1-git-send-email-armbru@redhat.com> Subject: [Qemu-devel] [PATCH 1/2] qxl: Fix SPICE_RING_PROD_ITEM(), SPICE_RING_CONS_ITEM() sanity check List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: alevy@redhat.com, kraxel@redhat.com From: Markus Armbruster The pointer arithmetic there is safe, but ugly. Coverity grouses about it. However, the actual comparison is off by one: <= end instead of < end. Fix by rewriting the check in a cleaner way. Signed-off-by: Markus Armbruster --- hw/qxl.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/hw/qxl.c b/hw/qxl.c index d08b9bd..2c2b422 100644 --- a/hw/qxl.c +++ b/hw/qxl.c @@ -37,33 +37,25 @@ */ #undef SPICE_RING_PROD_ITEM #define SPICE_RING_PROD_ITEM(qxl, r, ret) { \ - typeof(r) start = r; \ - typeof(r) end = r + 1; \ uint32_t prod = (r)->prod & SPICE_RING_INDEX_MASK(r); \ - typeof(&(r)->items[prod]) m_item = &(r)->items[prod]; \ - if (!((uint8_t*)m_item >= (uint8_t*)(start) && (uint8_t*)(m_item + 1) <= (uint8_t*)(end))) { \ + if (prod >= ARRAY_SIZE((r)->items)) { \ qxl_set_guest_bug(qxl, "SPICE_RING_PROD_ITEM indices mismatch " \ - "! %p <= %p < %p", (uint8_t *)start, \ - (uint8_t *)m_item, (uint8_t *)end); \ + "%u >= %zu", prod, ARRAY_SIZE((r)->items)); \ ret = NULL; \ } else { \ - ret = &m_item->el; \ + ret = &(r)->items[prod].el; \ } \ } #undef SPICE_RING_CONS_ITEM #define SPICE_RING_CONS_ITEM(qxl, r, ret) { \ - typeof(r) start = r; \ - typeof(r) end = r + 1; \ uint32_t cons = (r)->cons & SPICE_RING_INDEX_MASK(r); \ - typeof(&(r)->items[cons]) m_item = &(r)->items[cons]; \ - if (!((uint8_t*)m_item >= (uint8_t*)(start) && (uint8_t*)(m_item + 1) <= (uint8_t*)(end))) { \ + if (cons >= ARRAY_SIZE((r)->items)) { \ qxl_set_guest_bug(qxl, "SPICE_RING_CONS_ITEM indices mismatch " \ - "! %p <= %p < %p", (uint8_t *)start, \ - (uint8_t *)m_item, (uint8_t *)end); \ + "%u >= %zu", cons, ARRAY_SIZE((r)->items)); \ ret = NULL; \ } else { \ - ret = &m_item->el; \ + ret = &(r)->items[cons].el; \ } \ } -- 1.7.11.7