Skip to contentSkip to conversation

checkpatch: Check for invalid return codes

PATCH Superseded Prakruthi Deepak Heragu 2 participants 2 messages Jul 3, 2018 – Jul 4, 2018 Checkpatch

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpatch: Check for invalid return codes

The only valid integer return is 0, anything else
following "return" should be -ERRCODE or a function. Also, display context so that the user knows where the return value is incorrect.

http://lkml.org/lkml/2010/7/23/318
There's lots of "return -1;" statements in this patch - it's obscene that this is used to indicate "some error occurred" in kernel space rather than a real errno value - even when an existing function (eg, request_irq) gave you an error code already.

---
Changes in v1:
- Use CHK instead of ERROR
- Rephrase the warning message
- Provide the file name and line number where return value is incorrect

 scripts/checkpatch.pl | 6 ++++++
 1 file changed, 6 insertions(+)
scripts/checkpatch.pl +6 -0
index a9c0550..2808c27 100755
@@ -6197,6 +6197,12 @@ sub process {
 			     "switch default: should use break\n" . $herectx);
 		}
 
+# check for return codes on error paths
+		if ($line =~ /\breturn\s+-\d+/) {
+			CHK("NO_ERROR_CODE",
+			      "invalid return value, please return -<APPROPRIATE_ERRNO>\n" . $herecurr);
+		}
+
 # check for gcc specific __FUNCTION__
 		if ($line =~ /\b__FUNCTION__\b/) {
 			if (WARN("USE_FUNC",
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
maintainer
#1 lore raw
Quoted 2 lines · Prakruthi Deepak Heragu

The only valid integer return is 0, anything else following "return" should be -ERRCODE or a function.

Integer return values can be positive constant and correct.

So perhaps better:

Negative integer return codes should prefer to use #define -<ERRNO> values instead of negative numbers.

Quoted 8 lines

Also, display context
so that the user knows where the return value is incorrect.

http://lkml.org/lkml/2010/7/23/318
There's lots of "return -1;" statements in this patch - it's obscene that this is used to indicate "some error occurred" in kernel space rather than a real errno value - even when an existing function (eg, request_irq) gave you an error code already.

This bit is superfluous and if you are going to quote someone in a patch commit, it's nice to cc: them.

Quoted 3 lines

Does it really take 3 people to sign-off on this patch?

Quoted 14 lines

---
Changes in v1:
- Use CHK instead of ERROR
- Rephrase the warning message
- Provide the file name and line number where return value is incorrect

 scripts/checkpatch.pl | 6 ++++++
 1 file changed, 6 insertions(+)
scripts/checkpatch.pl +0 -0
index a9c0550..2808c27 100755

scripts/checkpatch.pl sub process { · line 6205

Quoted 9 lines
scripts/checkpatch.pl +3 -0
@@ -6197,6 +6197,12 @@ sub process {
 			     "switch default: should use break\n" . $herectx);
 		}
 
+# check for return codes on error paths
 
 
 
+		if ($line =~ /\breturn\s+-\d+/) {
+			CHK("NO_ERROR_CODE",

NO_ERROR_CODE isn't very obvious to me.
How about NEGATIVE_ERRNO or APPROPRIATE_ERRNO

Quoted 6 lines
scripts/checkpatch.pl +3 -0
+			      "invalid return value, please return -<APPROPRIATE_ERRNO>\n" . $herecurr);
+		}
+
 # check for gcc specific __FUNCTION__
scripts/checkpatch.pl +0 -0
 		if ($line =~ /\b__FUNCTION__\b/) {
 			if (WARN("USE_FUNC",