Received: by 10.223.176.5 with SMTP id f5csp1897368wra; Thu, 8 Feb 2018 05:24:14 -0800 (PST) X-Google-Smtp-Source: AH8x227c34VsrHoUAeNDBr6zn4wQqHO9xYDzwsqJvpVSPdGuC7tHx1i6HoXrpEsapWI+BP/m0C6W X-Received: by 2002:a17:902:4003:: with SMTP id b3-v6mr667784pld.154.1518096254635; Thu, 08 Feb 2018 05:24:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518096254; cv=none; d=google.com; s=arc-20160816; b=t0ZkmJGn5tYOkV242Mr9BTVQ5LO/qrurvcJflYB7L5u56a92+t4C1SCmCCKXf3JENK CVTJRn5zaGd7flSMwsdPFVJsndA9WYbrOzVb8fs/mJ0ACFfesTLmkBSOpPCQJni57JOZ OdzXmr311YMDfuM5f9As+NdEE/odS6s3gK7LeYD2b6ov0HpgwId7V9F4bkVi6hHAsM+U qhDZke0dgZF3Jvf4+lSZKc2h8YKgVM7LV8t3LM1aO3IBkB4dB7Zc3ti7NHi4sZxRGfnr 2IOOAac3EvUCTOf3QD3y45nRc+MhEyhfYpvqu96EXo49IwS+Kl3NgyLDYIXkxwk8JVK3 d8DQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=ukWzw0h7wbEzkORfaYrrmGpU0qWr8Y9/YWpgfeOgtF8=; b=YY7/qEsPMi1JjlJakzp3QSIf4qIhkp/DWaBClzfx82PS3h3o3MP6MXO68Gs3gBBuzC x+BjmylbsKCFvWIHDOg5DK31OrLDeOku9sn+RceuMet9KQ3PFWTy4RHMoUu+uKPBhdYd 2fX3pL8A/tbRX14k9RZ60sej8wvaYHoNAno4ytz4CCJnAkzivjnT2UmCT4zjTF+cTdGH F8TtHTpgometIOvEW2ZyDXqunEW9IdTjiUnrWqM60LFNE5nl5K4UFsaQ2I2RKnTOSL4q /9/ihRakQa8ogoCR1vhddKhcx5ubHhN2kxI92mC7w8qGIxN6oA/Yc2GSmpIX/oyD/kz+ fNFw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ba12-v6si1348590plb.352.2018.02.08.05.23.59; Thu, 08 Feb 2018 05:24:14 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751562AbeBHNXX (ORCPT + 99 others); Thu, 8 Feb 2018 08:23:23 -0500 Received: from mail3-relais-sop.national.inria.fr ([192.134.164.104]:60288 "EHLO mail3-relais-sop.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750876AbeBHNXV (ORCPT ); Thu, 8 Feb 2018 08:23:21 -0500 X-IronPort-AV: E=Sophos;i="5.46,478,1511823600"; d="scan'208";a="254077356" Received: from palace.lip6.fr (HELO localhost.localdomain) ([132.227.105.202]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/AES128-SHA256; 08 Feb 2018 14:23:19 +0100 From: Julia Lawall To: Masahiro Yamada , Dan Carpenter , Wolfram Sang Cc: kernel-janitors@vger.kernel.org, Gilles Muller , Nicolas Palix , Michal Marek , cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org Subject: [PATCH] scripts/coccinelle/misc/shift.cocci: detect < or > that should be a shift Date: Thu, 8 Feb 2018 13:53:54 +0100 Message-Id: <1518094434-12852-1-git-send-email-Julia.Lawall@lip6.fr> X-Mailer: git-send-email 1.9.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This checks for a comparison using < or > between two constants, considering both explicit constants (1, 2, etc) and macros defined with #define. False positives are possible in the latter case, when a macro may have multiple possible definitions and it is indeed necessary to check the value. There are currently two such false positives, in drivers/net/ethernet/chelsio/cxgb3/sge.c: q->fl[0].use_pages = FL0_PG_CHUNK_SIZE > 0; q->fl[1].use_pages = FL1_PG_CHUNK_SIZE > 0; It seems fairly clear that the use_pages field is intended to hold a boolean value. The semantic patch actually provides for this, but the rule requires the left side of the assignment to be a bool, and here it is declared as unsigned int. Hopefully there will not be too many false positives, and the benefit of including macros in the check will execeed the cost of checking the output for errors. Suggested-by: Dan Carpenter Suggested-by: Wolfram Sang Signed-off-by: Julia Lawall --- scripts/coccinelle/misc/shift.cocci | 89 ++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/scripts/coccinelle/misc/shift.cocci b/scripts/coccinelle/misc/shift.cocci new file mode 100644 index 0000000..15612f6 --- /dev/null +++ b/scripts/coccinelle/misc/shift.cocci @@ -0,0 +1,89 @@ +/// < and > between constants is typically meant to be a shift operation +//# When the left argument is a #define constant, the operation can be +//# meaningful. +/// +// Confidence: Moderate +// Copyright: (C) 2018 Julia Lawall, Inria, GPLv2. +// URL: http://coccinelle.lip6.fr/ +// Options: --include-headers --include-headers-for-types + +virtual patch +virtual context +virtual org +virtual report + +@ok@ +position p; +expression x,y; +binary operator op = {<,>}; +typedef bool; +bool b; +@@ + +( +x op@p y || ... +| +x op@p y && ... +| +b = x op@p y +| +WARN_ON(x op@p y) +| +WARN_ON_ONCE(x op@p y) +| +BUG_ON(x op@p y) +| +BUILD_BUG_ON(x op@p y) +| +BUILD_BUG_ON_MSG(x op@p y,...) +) + +// ---------------------------------------------------------------------------- + +@depends on patch && !context && !org && !report@ +type T; +binary operator op = {<,>}; +position p != ok.p; +constant int x, y; +@@ + +( + (T)x +- <@p ++ << + y +| + (T)x +- >@p ++ >> + y +) + +@r depends on !patch && (context || org || report)@ +type T; +binary operator op = {<,>}; +position p != ok.p; +constant int x, y; +position j0 : script:python() { j0[0].file != "" }; // "" in ifdefs +@@ + +*(T)x op@p@j0 y + +// ---------------------------------------------------------------------------- + +@script:python r_org depends on org@ +j0 << r.j0; +@@ + +msg = "WARNING: Shift rather than comparison expected." +coccilib.org.print_todo(j0[0], msg) + +// ---------------------------------------------------------------------------- + +@script:python r_report depends on report@ +j0 << r.j0; +@@ + +msg = "WARNING: Shift rather than comparison expected." +coccilib.report.print_report(j0[0], msg) +