public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] checkpatch: Flag code that returns a negative number
@ 2016-06-03 15:25 Nishanth Menon
  2016-06-03 15:41 ` Joe Perches
  2016-06-03 20:02 ` [PATCH V2] checkpatch: Flag code that returns a negative number less than 1 Nishanth Menon
  0 siblings, 2 replies; 9+ messages in thread
From: Nishanth Menon @ 2016-06-03 15:25 UTC (permalink / raw)
  To: Joe Perches, Andy Whitcroft; +Cc: linux-kernel, Nishanth Menon

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.

Signed-off-by: Nishanth Menon <nm@ti.com>
---
 scripts/checkpatch.pl | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 4904ced676d4..f6fa07fe33a5 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 (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:"
-- 
2.8.0

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2016-06-03 20:46 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox