Skip to contentSkip to conversation

checkpatch: Check for illegal return codes

PATCH In review Prakruthi Deepak Heragu 2 participants 4 messages Jul 3, 2018 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpatch: Check for illegal return codes

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

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.

---
 scripts/checkpatch.pl | 6 ++++++
 1 file changed, 6 insertions(+)
scripts/checkpatch.pl +6 -0
index a9c0550..260d252 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+/) {
+			ERROR("NO_ERROR_CODE",
+			      "illegal return value, please use an error code");
+		}
+
 # 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 21 lines · Prakruthi Deepak Heragu

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

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.

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

scripts/checkpatch.pl sub process { · line 6205

Quoted 9 lines
scripts/checkpatch.pl +6 -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+/) {
+			ERROR("NO_ERROR_CODE",
+			      "illegal return value, please use an error code");
+		}
+

Substitute illegal to invalid as this wouldn't be illegal. It might be invalid and this needs a newline and $herecurr

I'm not sure this is even useful.

There are _way_ too many of these already existing and simple return identifiers can be OK.

$ git grep -P '\breturn\s*\-(?!0)\d+' | wc -l
10193

and

$ git grep -P '\breturn\s*\-(?!1)\d+' | wc -l
240
#2 lore raw
Quoted 40 lines · Joe Perches
Quoted 21 lines · Prakruthi Deepak Heragu

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

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.

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

scripts/checkpatch.pl sub process { · line 6205

Quoted 9 lines
scripts/checkpatch.pl +6 -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+/) {
+			ERROR("NO_ERROR_CODE",
+			      "illegal return value, please use an error code");
+		}
+

Substitute illegal to invalid as this wouldn't be illegal. It might be invalid and this needs a newline and $herecurr

I'm not sure this is even useful.

There are _way_ too many of these already existing and simple return identifiers can be OK.

Quoted 7 lines
$ git grep -P '\breturn\s*\-(?!0)\d+' | wc -l
10193

and

$ git grep -P '\breturn\s*\-(?!1)\d+' | wc -l
240

True. However, this would be helpful to avoid usage of such return statements in the future.

maintainer
#3 lore raw
Quoted 10 lines · On Tue, 2018-07-03 at 12:07 -0700, [email protected] wrote:
Quoted 9 lines · Joe Perches
Quoted 8 lines · Prakruthi Deepak Heragu

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

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.

[]

Quoted 1 line
Quoted 1 line

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 22 lines
Quoted 22 lines
(quoted diff) +0 -0
@@ -6197,6 +6197,12 @@ sub process {
 >  			     "switch default: should use break\n" . $herectx);
 >  		}

scripts/checkpatch.pl sub process { · line 6205

Quoted 7 lines
scripts/checkpatch.pl +6 -0
+# check for return codes on error paths
+		if ($line =~ /\breturn\s+-\d+/) {
+			ERROR("NO_ERROR_CODE",
+			      "illegal return value, please use an error code");
+		}
+

Substitute illegal to invalid as this wouldn't be illegal. It might be invalid and this needs a newline and $herecurr

I'm not sure this is even useful.

There are _way_ too many of these already existing and simple return identifiers can be OK.

$ git grep -P '\breturn\s*\-(?!0)\d+' | wc -l
10193
Quoted 7 lines
Quoted 4 lines

and

$ git grep -P '\breturn\s*\-(?!1)\d+' | wc -l
240

True. However, this would be helpful to avoid usage of such return statements in the future.

Maybe use CHK and not ERROR and maybe the output message could be something like "Prefer 'return -<APPROPRIATE_ERRNO>'"