]> www.infradead.org Git - users/hch/misc.git/commitdiff
checkpatch: suppress strscpy warnings for userspace tools
authorSuchit Karunakaran <suchitkarunakaran@gmail.com>
Tue, 23 Sep 2025 17:17:21 +0000 (22:47 +0530)
committerAndrew Morton <akpm@linux-foundation.org>
Sun, 28 Sep 2025 18:36:13 +0000 (11:36 -0700)
The checkpatch.pl script currently warns against the use of strcpy,
strlcpy, and strncpy, recommending strscpy as a safer alternative.
However, these warnings are also triggered for code under tools/ and
scripts/, which are userspace utilities where strscpy is not available.
This patch suppresses these warnings for files in tools/ and scripts/.

Link: https://lkml.kernel.org/r/20250923171722.7798-1-suchitkarunakaran@gmail.com
Signed-off-by: Suchit Karunakaran <suchitkarunakaran@gmail.com>
Acked-by: Joe Perches <joe@perches.com>
Cc: Andy Whitcroft <apw@canonical.com>
Cc: Dwaipayan Ray <dwaipayanray1@gmail.com>
Cc: Lukas Bulwahn <lukas.bulwahn@gmail.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
scripts/checkpatch.pl

index 319cc5f858858b23bb9516c7ad7c4a9800770f80..92669904eecc7a8d2afd3f2625528e02b6d17cd6 100755 (executable)
@@ -2636,6 +2636,11 @@ sub exclude_global_initialisers {
                $realfile =~ m@/bpf/.*\.bpf\.c$@;
 }
 
+sub is_userspace {
+    my ($realfile) = @_;
+    return ($realfile =~ m@^tools/@ || $realfile =~ m@^scripts/@);
+}
+
 sub process {
        my $filename = shift;
 
@@ -7018,21 +7023,20 @@ sub process {
 #                              }
 #                      }
 #              }
-
 # strcpy uses that should likely be strscpy
-               if ($line =~ /\bstrcpy\s*\(/) {
+               if ($line =~ /\bstrcpy\s*\(/ && !is_userspace($realfile)) {
                        WARN("STRCPY",
                             "Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88\n" . $herecurr);
                }
 
 # strlcpy uses that should likely be strscpy
-               if ($line =~ /\bstrlcpy\s*\(/) {
+               if ($line =~ /\bstrlcpy\s*\(/ && !is_userspace($realfile)) {
                        WARN("STRLCPY",
                             "Prefer strscpy over strlcpy - see: https://github.com/KSPP/linux/issues/89\n" . $herecurr);
                }
 
 # strncpy uses that should likely be strscpy or strscpy_pad
-               if ($line =~ /\bstrncpy\s*\(/) {
+               if ($line =~ /\bstrncpy\s*\(/ && !is_userspace($realfile)) {
                        WARN("STRNCPY",
                             "Prefer strscpy, strscpy_pad, or __nonstring over strncpy - see: https://github.com/KSPP/linux/issues/90\n" . $herecurr);
                }