From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932302AbcFCQBM (ORCPT ); Fri, 3 Jun 2016 12:01:12 -0400 Received: from smtprelay0198.hostedemail.com ([216.40.44.198]:58100 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751485AbcFCQBK (ORCPT ); Fri, 3 Jun 2016 12:01:10 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::,RULES_HIT:41:355:379:541:560:599:960:973:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:1981:2194:2199:2393:2553:2559:2562:2828:3138:3139:3140:3141:3142:3354:3622:3865:3866:3867:3868:3870:3871:3872:3874:4321:5007:7903:7974:10004:10400:10848:11232:11658:11914:12043:12517:12519:12740:13069:13132:13231:13311:13357:13439:14659:14721:21080:21324:30012:30034:30054:30064:30069:30070:30090:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:1,LUA_SUMMARY:none X-HE-Tag: patch86_60d9ee6ac9603 X-Filterd-Recvd-Size: 2756 Message-ID: <1464969664.11800.9.camel@perches.com> Subject: Re: [PATCH] checkpatch: Flag code that returns a negative number From: Joe Perches To: "Andrew F. Davis" , Nishanth Menon , Andy Whitcroft Cc: linux-kernel@vger.kernel.org, Andrew Morton Date: Fri, 03 Jun 2016 09:01:04 -0700 In-Reply-To: <5751A6F7.1030305@ti.com> References: <1464967533-14634-1-git-send-email-nm@ti.com> <1464968493.11800.3.camel@perches.com> <5751A6F7.1030305@ti.com> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.18.5.2-0ubuntu3 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2016-06-03 at 10:49 -0500, Andrew F. Davis wrote: > On 06/03/2016 10:41 AM, Joe Perches wrote: > > On Fri, 2016-06-03 at 10:25 -0500, Nishanth Menon wrote: > > > 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 > > > > > > Considering typical newbie error of doing the following: > > > int fn(void) > > > { > > > /* ... error condition ... */ > > > return -1; > > > } > > > > > > 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. > > I think it's not a newbie error to have a -1 return and it > > seems like rather too many cases to even suggest be changed. > > > > $ git grep -E "\breturn\s+\-\s*[0-9]+" * | grep -v "^tools" | wc -l > > 8388 > > > A quick look over some of these cases show many *should* be replaced > with proper error codes. > > Removing the simple -1 case, which is often used for signaling one level > up of an error, gives better results though: > > $ git grep -E "\breturn\s+\-\s*[2-9][0-9]*" * | grep -v "^tools" | wc -l > 189 I did more or less the same grep, and that's somewhat true. -1 though is very common and doesn't need to be replaced. $ git grep -E "\breturn\s+\-\s*[0-9]+\s*;" * | grep -v "^tools" | grep -vP "return\s*\-1;" | wc -l 211 Looking at some of the specific instances of negative return values instead of the line counts though may show otherwise. -EFOO errors aren't always better.