From patchwork Fri Jun 13 21:18:50 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adam Buchbinder X-Patchwork-Id: 4351721 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0EAE09F314 for ; Fri, 13 Jun 2014 21:19:04 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1DB5C2026D for ; Fri, 13 Jun 2014 21:19:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 53EEE20268 for ; Fri, 13 Jun 2014 21:19:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751961AbaFMVS7 (ORCPT ); Fri, 13 Jun 2014 17:18:59 -0400 Received: from mail-pb0-f54.google.com ([209.85.160.54]:46582 "EHLO mail-pb0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751811AbaFMVS6 (ORCPT ); Fri, 13 Jun 2014 17:18:58 -0400 Received: by mail-pb0-f54.google.com with SMTP id jt11so2519031pbb.27 for ; Fri, 13 Jun 2014 14:18:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=g5Uhp4PeCkbLUzcq3UmzR9Vf05HvfMM7RVACioXqGCI=; b=N1q4jzpwqCK8fvxB31ryJZzqeEFVCwo+6U0TUgZYr9nHD5xkupAvQarMK/RSJd+Afr wnyApZPRZ1w9J/WXO4ww1cRwwUCvQEuUtgaeO7amd7/+/Xj3K1ZiL4mTNLaSmN4884r4 636UlsuLG2y4aJLOJTY8uJs+uhwN3SokkJW3DRxQOiJ/nEk0/KcTXPYZRdsmMLdfG6o0 zc4rA1oi/BurHK6ZO687TURasN4xd4HHwv6kEZ6emOVcntZRBkVoTO2jjW5+ezKphJaU SnlAueutv6OTNckYPZNHzl3jaU89mBdLDTmIM2JyTuWteqrtTwjb1fwKixZyDqaYQZCS USsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=g5Uhp4PeCkbLUzcq3UmzR9Vf05HvfMM7RVACioXqGCI=; b=BWzCrL1LQRQUnMuWXvULDhcEjE+nOddi15FAX1HPbitL+zrN4BK9Z256tPYszlMO2+ +nGRXZbbIOtyCHQQIijW/jRlqg4MLG9IPA7lGNe6fJkgs3Wo38vuimFTL6jyuOOmJTHL +jXmvvHem5CvaRxYodF+vx3TFsMdNktNTN5ZdneWFlUkonUG896aIH3D3B2cRwlqP0ZA PZ/Z0xiiZK6+85n5CfhWMvHeeqNE0Zm36GaMx+2dBc9y4etXFCCpHwFWmqBfFKRmyl5T 2l6NmJfsA25ygc4+1eAGpX2fcTwj9mpz2gkFiz+USa3uxZcftTgpBlAtGZTye28yDh7G ETtA== X-Gm-Message-State: ALoCoQkfmmWIZbIY7nsXrW2urixg1U5BGtJatTROvgLEZ7gQ7ldoSPtqK+y0b6T8sOEkqagadSycAbVGbdRf9af+Se/RwtZbyY3Nqm/0+Kg7FVJsBBgArsD9hgE9lchjdwpHBaCHhTCAExunZiGbGbjkvXrhs2YJDg== X-Received: by 10.66.102.4 with SMTP id fk4mr6237774pab.59.1402694338159; Fri, 13 Jun 2014 14:18:58 -0700 (PDT) Received: from abuchbinder-glaptop.corp.google.com (dhcp-172-19-65-142.mtv.corp.google.com [172.19.65.142]) by mx.google.com with ESMTPSA id uj3sm26680512pac.8.2014.06.13.14.18.57 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 13 Jun 2014 14:18:57 -0700 (PDT) From: Adam Buchbinder To: linux-btrfs@vger.kernel.org Cc: dave@jikos.cz, Adam Buchbinder Subject: [PATCH] Fix undefined behavior in radix-tree.c. Date: Fri, 13 Jun 2014 14:18:50 -0700 Message-Id: <1402694330-21211-1-git-send-email-abuchbinder@google.com> X-Mailer: git-send-email 2.0.0.526.g5318336 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When running with UndefinedBehaviorSanitizer, the tests produce the following error: radix-tree.c:836:30: runtime error: shift exponent 18446744073709551613 is too large for 64-bit type 'unsigned long' (That's a negative shift exponent represented as an unsigned long.) Even though the value is discarded in those cases, it's still undefined behavior; see the C99 standard, section 6.5.7, paragraph three: "If the value of the right operand is negative [...] the behavior is undefined." Signed-off-by: Adam Buchbinder Reviewed-by: Satoru Takeuchi --- radix-tree.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/radix-tree.c b/radix-tree.c index 4f295fc..7457944 100644 --- a/radix-tree.c +++ b/radix-tree.c @@ -833,10 +833,10 @@ int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag) static unsigned long __maxindex(unsigned int height) { unsigned int tmp = height * RADIX_TREE_MAP_SHIFT; - unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1; + unsigned long index = ~0UL; - if (tmp >= RADIX_TREE_INDEX_BITS) - index = ~0UL; + if (tmp < RADIX_TREE_INDEX_BITS) + index = (index >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1; return index; }