Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1413316

Re: [PATCH] checkpatch: Flag code that returns a negative number

From "Andrew F. Davis" <afd@ti.com>
Newsgroups linux.kernel
Subject Re: [PATCH] checkpatch: Flag code that returns a negative number
Date 2016-06-03 17:50 +0200
Message-ID <rFZwd-57d-9@gated-at.bofh.it> (permalink)
References <rFZcS-4YZ-13@gated-at.bofh.it> <rFZwe-57d-11@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw



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

Andrew

> 
>> diff --git 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 (defined($stat) && $stat =~ /^.\s*return\s*-[0-9]+\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:"

Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH] checkpatch: Flag code that returns a negative number Nishanth Menon <nm@ti.com> - 2016-06-03 17:30 +0200
  Re: [PATCH] checkpatch: Flag code that returns a negative number "Andrew F. Davis" <afd@ti.com> - 2016-06-03 17:50 +0200
    Re: [PATCH] checkpatch: Flag code that returns a negative number Joe Perches <joe@perches.com> - 2016-06-03 18:10 +0200
      Re: [PATCH] checkpatch: Flag code that returns a negative number Nishanth Menon <nm@ti.com> - 2016-06-03 18:10 +0200
        Re: [PATCH] checkpatch: Flag code that returns a negative number Joe Perches <joe@perches.com> - 2016-06-03 18:20 +0200
  Re: [PATCH] checkpatch: Flag code that returns a negative number Joe Perches <joe@perches.com> - 2016-06-03 17:50 +0200
  [PATCH V2] checkpatch: Flag code that returns a negative number less than 1 Nishanth Menon <nm@ti.com> - 2016-06-03 22:10 +0200
    Re: [PATCH V2] checkpatch: Flag code that returns a negative number  less than 1 Joe Perches <joe@perches.com> - 2016-06-03 22:50 +0200
      Re: [PATCH V2] checkpatch: Flag code that returns a negative number  less than 1 Nishanth Menon <nm@ti.com> - 2016-06-03 22:50 +0200

csiph-web