From patchwork Fri Jan 20 13:31:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 717709 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3v4jGl5fnfz9snk for ; Sat, 21 Jan 2017 01:08:15 +1100 (AEDT) Received: from localhost ([::1]:54970 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cUZrR-0000eK-9O for incoming@patchwork.ozlabs.org; Fri, 20 Jan 2017 09:08:13 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53766) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cUZIo-0004Ol-3w for qemu-devel@nongnu.org; Fri, 20 Jan 2017 08:32:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cUZIl-0006fb-4g for qemu-devel@nongnu.org; Fri, 20 Jan 2017 08:32:26 -0500 Received: from mx1.redhat.com ([209.132.183.28]:59236) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cUZIk-0006ex-Vw for qemu-devel@nongnu.org; Fri, 20 Jan 2017 08:32:23 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EABABC04BD20; Fri, 20 Jan 2017 13:32:22 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-138.ams2.redhat.com [10.36.117.138]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v0KDVeVg014401; Fri, 20 Jan 2017 08:32:21 -0500 From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 20 Jan 2017 14:31:38 +0100 Message-Id: <20170120133139.31080-35-pbonzini@redhat.com> In-Reply-To: <20170120133139.31080-1-pbonzini@redhat.com> References: <20170120133139.31080-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Fri, 20 Jan 2017 13:32:23 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 34/35] 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 Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Peter Lieven 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 Reviewed-by: Fam Zheng Signed-off-by: Peter Lieven Message-Id: <1484844230-24490-1-git-send-email-pl@kamp.de> Signed-off-by: Paolo Bonzini --- 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);