Re: [PATCH] err_ptr.h: introduce ERR_PTR_SAFE()

Andreas Dilger <[email protected]> Sun, 17 May 2026 03:13:00 -0600
Newsgroups org.kernel.vger.linux-unionfs,org.kernel.vger.linux-fsdevel,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
On Thu, 14 May 2026 22:01:29 +0200 Amir Goldstein <[email protected]> wrote:
> 
> The check for constants may be fairly pointless.
> One of the static checkers may already detect the obvious fubar ERR_PTR(EINVAL).  


Actually, I just ran across an issue that checkpatch.pl does *not* detect
this "obvious" case.  It complains about "return EINVAL", but does not say
anything for cases like "return ERR_PTR(EINVAL)" or "rc = EINVAL; return rc;".

The following patch fixes checkpatch.pl to report many more such cases, and
has very few false positives for checking common error return assignments.

diff --git a/contrib/scripts/checkpatch.pl b/contrib/scripts/checkpatch.pl
index 70c78a3..e3fdedf 100755
--- a/contrib/scripts/checkpatch.pl
+++ b/contrib/scripts/checkpatch.pl
@@ -5795,11 +5795,12 @@
  		}
 
 # Return of what appears to be an errno should normally be negative
-		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
-			my $name = $1;
+		if (!is_userspace($realfile) &&
+		    $sline =~ /\b(?i)(return|err =|rc =|ret =|retval =|ERR_PTR)(?-i)(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
+			my $name = $2;
 			if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) {
 				WARN("USE_NEGATIVE_ERRNO",
-				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
+				     "return of an errno should typically be negative (ie: $1 -$2)\n" . $herecurr);
 	}
 }
 

Cheers, Andreas