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


Groups > linux.kernel > #1413437

[PATCH V2] checkpatch: Flag code that returns a negative number less than 1

From Nishanth Menon <nm@ti.com>
Newsgroups linux.kernel
Subject [PATCH V2] checkpatch: Flag code that returns a negative number less than 1
Date 2016-06-03 22:10 +0200
Message-ID <rG3zQ-7MZ-17@gated-at.bofh.it> (permalink)
References <rFZcS-4YZ-13@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


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 <joe@perches.com>
Signed-off-by: Nishanth Menon <nm@ti.com>
---
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(+)

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:"
-- 
2.8.0

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