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

From: Andreas Dilger

Date: Sun May 17 2026 - 05:13:25 EST


On Thu, 14 May 2026 22:01:29 +0200 Amir Goldstein <amir73il@xxxxxxxxx> 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