public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: "Andrew F. Davis" <afd@ti.com>, Nishanth Menon <nm@ti.com>,
	Andy Whitcroft <apw@canonical.com>
Cc: linux-kernel@vger.kernel.org, Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [PATCH] checkpatch: Flag code that returns a negative number
Date: Fri, 03 Jun 2016 09:01:04 -0700	[thread overview]
Message-ID: <1464969664.11800.9.camel@perches.com> (raw)
In-Reply-To: <5751A6F7.1030305@ti.com>

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.

  reply	other threads:[~2016-06-03 16:01 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-03 15:25 [PATCH] checkpatch: Flag code that returns a negative number Nishanth Menon
2016-06-03 15:41 ` Joe Perches
2016-06-03 15:49   ` Andrew F. Davis
2016-06-03 16:01     ` Joe Perches [this message]
2016-06-03 16:07       ` Nishanth Menon
2016-06-03 16:16         ` Joe Perches
2016-06-03 20:02 ` [PATCH V2] checkpatch: Flag code that returns a negative number less than 1 Nishanth Menon
2016-06-03 20:42   ` Joe Perches
2016-06-03 20:46     ` Nishanth Menon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1464969664.11800.9.camel@perches.com \
    --to=joe@perches.com \
    --cc=afd@ti.com \
    --cc=akpm@linux-foundation.org \
    --cc=apw@canonical.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nm@ti.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox