aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorJoe Perches <joe@perches.com>2016-10-11 16:51:47 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-10-11 18:06:30 -0400
commitf90774e1fd2700de4a6e0d62866d34a26c544bd0 (patch)
tree4318a68ad5b45d1246138d98d82c0519bad4f989 /scripts
parent85b0ee18bbf82cb3e1880c718749149c6ed61058 (diff)
checkpatch: look for symbolic permissions and suggest octal instead
S_<FOO> uses should be avoided where octal is more intelligible. Linus didst say: : It's *much* easier to parse and understand the octal numbers, while the : symbolic macro names are just random line noise and hard as hell to : understand. You really have to think about it. : : So we should rather go the other way: convert existing bad symbolic : permission bit macro use to just use the octal numbers. : : The symbolic names are good for the *other* bits (ie sticky bit, and the : inode mode _type_ numbers etc), but for the permission bits, the symbolic : names are just insane crap. Nobody sane should ever use them. Not in the : kernel, not in user space. (http://lkml.kernel.org/r/CA+55aFw5v23T-zvDZp-MmD_EYxF8WbafwwB59934FV7g21uMGQ@mail.gmail.com) Link: http://lkml.kernel.org/r/7232ef011d05a92f4caa86a5e9830d87966a2eaf.1470180926.git.joe@perches.com Signed-off-by: Joe Perches <joe@perches.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/checkpatch.pl49
1 files changed, 43 insertions, 6 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 34b445574011..1c82b01d581a 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -541,6 +541,32 @@ our $mode_perms_world_writable = qr{
541 0[0-7][0-7][2367] 541 0[0-7][0-7][2367]
542}x; 542}x;
543 543
544our %mode_permission_string_types = (
545 "S_IRWXU" => 0700,
546 "S_IRUSR" => 0400,
547 "S_IWUSR" => 0200,
548 "S_IXUSR" => 0100,
549 "S_IRWXG" => 0070,
550 "S_IRGRP" => 0040,
551 "S_IWGRP" => 0020,
552 "S_IXGRP" => 0010,
553 "S_IRWXO" => 0007,
554 "S_IROTH" => 0004,
555 "S_IWOTH" => 0002,
556 "S_IXOTH" => 0001,
557 "S_IRWXUGO" => 0777,
558 "S_IRUGO" => 0444,
559 "S_IWUGO" => 0222,
560 "S_IXUGO" => 0111,
561);
562
563#Create a search pattern for all these strings to speed up a loop below
564our $mode_perms_string_search = "";
565foreach my $entry (keys %mode_permission_string_types) {
566 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
567 $mode_perms_string_search .= $entry;
568}
569
544our $allowed_asm_includes = qr{(?x: 570our $allowed_asm_includes = qr{(?x:
545 irq| 571 irq|
546 memory| 572 memory|
@@ -6003,20 +6029,31 @@ sub process {
6003 $arg_pos--; 6029 $arg_pos--;
6004 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 6030 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6005 } 6031 }
6006 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]"; 6032 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6007 if ($line =~ /$test/) { 6033 if ($line =~ /$test/) {
6008 my $val = $1; 6034 my $val = $1;
6009 $val = $6 if ($skip_args ne ""); 6035 $val = $6 if ($skip_args ne "");
6010 6036 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6011 if ($val !~ /^0$/ && 6037 ($val =~ /^$Octal$/ && length($val) ne 4)) {
6012 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6013 length($val) ne 4)) {
6014 ERROR("NON_OCTAL_PERMISSIONS", 6038 ERROR("NON_OCTAL_PERMISSIONS",
6015 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr); 6039 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
6016 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) { 6040 }
6041 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6017 ERROR("EXPORTED_WORLD_WRITABLE", 6042 ERROR("EXPORTED_WORLD_WRITABLE",
6018 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 6043 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
6019 } 6044 }
6045 if ($val =~ /\b$mode_perms_string_search\b/) {
6046 my $to = 0;
6047 while ($val =~ /\b($mode_perms_string_search)\b(?:\s*\|\s*)?\s*/g) {
6048 $to |= $mode_permission_string_types{$1};
6049 }
6050 my $new = sprintf("%04o", $to);
6051 if (WARN("SYMBOLIC_PERMS",
6052 "Symbolic permissions are not preferred. Consider using octal permissions $new.\n" . $herecurr) &&
6053 $fix) {
6054 $fixed[$fixlinenr] =~ s/\Q$val\E/$new/;
6055 }
6056 }
6020 } 6057 }
6021 } 6058 }
6022 } 6059 }