From patchwork Fri Jun 3 20:02:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nishanth Menon X-Patchwork-Id: 69274 Delivered-To: patch@linaro.org Received: by 10.140.106.246 with SMTP id e109csp431917qgf; Fri, 3 Jun 2016 13:02:56 -0700 (PDT) X-Received: by 10.66.183.69 with SMTP id ek5mr7125750pac.153.1464984176380; Fri, 03 Jun 2016 13:02:56 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m132si9331219pfc.122.2016.06.03.13.02.54; Fri, 03 Jun 2016 13:02:56 -0700 (PDT) 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 S964809AbcFCUCp (ORCPT + 31 others); Fri, 3 Jun 2016 16:02:45 -0400 Received: from comal.ext.ti.com ([198.47.26.152]:33463 "EHLO comal.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751544AbcFCUCh (ORCPT ); Fri, 3 Jun 2016 16:02:37 -0400 Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by comal.ext.ti.com (8.13.7/8.13.7) with ESMTP id u53K2QBo003178; Fri, 3 Jun 2016 15:02:26 -0500 Received: from DLEE70.ent.ti.com (dlee70.ent.ti.com [157.170.170.113]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id u53K2Wb6007776; Fri, 3 Jun 2016 15:02:32 -0500 Received: from dlep33.itg.ti.com (157.170.170.75) by DLEE70.ent.ti.com (157.170.170.113) with Microsoft SMTP Server id 14.3.294.0; Fri, 3 Jun 2016 15:02:32 -0500 Received: from localhost (ileax41-snat.itg.ti.com [10.172.224.153]) by dlep33.itg.ti.com (8.14.3/8.13.8) with ESMTP id u53K2Wh0012805; Fri, 3 Jun 2016 15:02:32 -0500 From: Nishanth Menon To: Joe Perches , Andy Whitcroft CC: , , Nishanth Menon Subject: [PATCH V2] checkpatch: Flag code that returns a negative number less than 1 Date: Fri, 3 Jun 2016 15:02:02 -0500 Message-ID: <1464984122-16381-1-git-send-email-nm@ti.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1464967533-14634-1-git-send-email-nm@ti.com> References: <1464967533-14634-1-git-send-email-nm@ti.com> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In some functions, returning a -ve decimal value is actually a valid return condition when the function is returning a value, however, it can also be misused for returning an error value that should ideally be a valid error code defined in include/uapi/asm-generic/errno-base.h or include/uapi/asm-generic/errno.h. The notable exception is "-1" which has quiet a history of usage as pointed out by Joe Perches. Considering typical error of doing the following: int fn(void) { /* ... error condition ... */ return -2; } void fn1(void) { /* some code */ if (fn() < 0) { pr_err("Error occurred\n"); return; } /* other cases... */ } Flag this as a check case for developer verification. The check is done for negative values less than 1 and tools directory is exempt from this requirement based on Joe Perches' suggestion. Suggested-by: Joe Perches Signed-off-by: Nishanth Menon --- Changes in V2: - change in regex for check for check for less than 1 - Update in commit message to the effect - Added Suggested-by for Joe's recommendation on regex. V1: https://patchwork.kernel.org/patch/9153345/ scripts/checkpatch.pl | 6 ++++++ 1 file changed, 6 insertions(+) -- 2.8.0 diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 4904ced676d4..a2e677b5fd78 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -4351,6 +4351,12 @@ sub process { } } +# return with a value is not usually a good sign, unless the function is supposed to return a value + if ($realfile !~ /^tools/ && defined($stat) && $stat =~ /^.\s*return\s*-\s*(?!1\b)\d+\s*;/s) { + CHK("RETURN_NUMBER", + "Suspect error return with a value, If this is error value, refer to include/uapi/asm-generic/errno-base.h and include/uapi/asm-generic/errno.h\n" . $herecurr); + } + # unnecessary return in a void function # at end-of-function, with the previous line a single leading tab, then return; # and the line before that not a goto label target like "out:"