From patchwork Thu Jan 19 16:43:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Lieven X-Patchwork-Id: 9526389 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C91D66020B for ; Thu, 19 Jan 2017 16:46:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BB70D284DC for ; Thu, 19 Jan 2017 16:46:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B08AD285A8; Thu, 19 Jan 2017 16:46:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 24ADA284DC for ; Thu, 19 Jan 2017 16:46:32 +0000 (UTC) Received: from localhost ([::1]:49581 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cUFr5-00050r-At for patchwork-qemu-devel@patchwork.kernel.org; Thu, 19 Jan 2017 11:46:31 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33521) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cUFof-0003mW-Vo for qemu-devel@nongnu.org; Thu, 19 Jan 2017 11:44:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cUFob-0004Kc-0A for qemu-devel@nongnu.org; Thu, 19 Jan 2017 11:44:02 -0500 Received: from mx-v6.kamp.de ([2a02:248:0:51::16]:34979 helo=mx01.kamp.de) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cUFoa-0004Am-Nl for qemu-devel@nongnu.org; Thu, 19 Jan 2017 11:43:56 -0500 Received: (qmail 12920 invoked by uid 89); 19 Jan 2017 16:43:53 -0000 Received: from [195.62.97.28] by client-16-kamp (envelope-from , uid 89) with qmail-scanner-2010/03/19-MF (clamdscan: 0.99.2/22913. avast: 1.2.2/17010300. spamassassin: 3.4.1. Clear:RC:1(195.62.97.28):. Processed in 0.039925 secs); 19 Jan 2017 16:43:53 -0000 Received: from smtp.kamp.de (HELO submission.kamp.de) ([195.62.97.28]) by mx01.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted); 19 Jan 2017 16:43:52 -0000 X-GL_Whitelist: yes Received: (qmail 4461 invoked from network); 19 Jan 2017 16:43:52 -0000 Received: from lieven-pc.kamp-intra.net (HELO lieven-pc) (relay@kamp.de@::ffff:172.21.12.60) by submission.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted) ESMTPA; 19 Jan 2017 16:43:52 -0000 Received: by lieven-pc (Postfix, from userid 1000) id 9019A20A89; Thu, 19 Jan 2017 17:43:52 +0100 (CET) From: Peter Lieven To: qemu-devel@nongnu.org Date: Thu, 19 Jan 2017 17:43:50 +0100 Message-Id: <1484844230-24490-1-git-send-email-pl@kamp.de> X-Mailer: git-send-email 1.9.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a02:248:0:51::16 Subject: [Qemu-devel] [PATCH] bitmap: assert that start and nr are non negative X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Lieven , pbonzini@redhat.com, famz@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP commit e1123a3b introduced a data corruption regression in the iscsi driver because it passed -1 as nr to bitmap_set and bitmap_clear. Add an assertion to catch such flaws earlier. Suggested-by: Fam Zheng Signed-off-by: Peter Lieven Reviewed-by: Fam Zheng --- util/bitmap.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/util/bitmap.c b/util/bitmap.c index 43ed011..c1a84ca 100644 --- a/util/bitmap.c +++ b/util/bitmap.c @@ -164,6 +164,8 @@ void bitmap_set(unsigned long *map, long start, long nr) int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start); + assert(start >= 0 && nr >= 0); + while (nr - bits_to_set >= 0) { *p |= mask_to_set; nr -= bits_to_set; @@ -184,6 +186,8 @@ void bitmap_set_atomic(unsigned long *map, long start, long nr) int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start); + assert(start >= 0 && nr >= 0); + /* First word */ if (nr - bits_to_set > 0) { atomic_or(p, mask_to_set); @@ -221,6 +225,8 @@ void bitmap_clear(unsigned long *map, long start, long nr) int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start); + assert(start >= 0 && nr >= 0); + while (nr - bits_to_clear >= 0) { *p &= ~mask_to_clear; nr -= bits_to_clear; @@ -243,6 +249,8 @@ bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr) unsigned long dirty = 0; unsigned long old_bits; + assert(start >= 0 && nr >= 0); + /* First word */ if (nr - bits_to_clear > 0) { old_bits = atomic_fetch_and(p, ~mask_to_clear);