[PATCH 4/4] checkpatch: fail on missing or unreadable files with --file

From: Bjorn Andersson

Date: Sat Nov 07 2026 - 23:23:59 EST


With --file, checkpatch reads each input through "diff -u /dev/null"
and doesn't check whether diff managed to read the file. A file that
doesn't exist or can't be read therefore produces no output and is
silently treated as an empty, and thereby clean, input. Only diff's
own error message hints at the problem, and the exit status reports
success:

$ ./scripts/checkpatch.pl -q --terse -f missing.c
diff: missing.c: No such file or directory
$ echo $?
0

This differs from patch mode, where an input that can't be opened is
treated as a fatal error.

Open the file before running diff, and fail in the same way as patch
mode does when that isn't possible:

$ ./scripts/checkpatch.pl -q --terse -f missing.c
./scripts/checkpatch.pl: missing.c: open failed - No such file or directory

Signed-off-by: Bjorn Andersson <bjorn.andersson@xxxxxxxxxxxxxxxx>
---
scripts/checkpatch.pl | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index a5e6f96477c3..7233a19329f8 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1346,6 +1346,12 @@ for my $filename (@ARGV) {
open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
die "$P: $filename: git format-patch failed - $!\n";
} elsif ($file) {
+ # diff failing to read the file would look like an empty file
+ if ($filename ne '-') {
+ open(my $fh, '<', $filename) ||
+ die "$P: $filename: open failed - $!\n";
+ close($fh);
+ }
open($FILE, '-|', "diff -u /dev/null $filename") ||
die "$P: $filename: diff failed - $!\n";
} elsif ($filename eq '-') {

--
2.55.0