History

checkpatch: add checks for constant non-octal permissions

umode_t permissions are sometimes mistakenly written with decimal constants. Verify that numeric permissions are using octal. Add a list of the most commonly used functions and macros that have umode_t permissions and the argument position. Add a $Octal type to $Constant. Allow $LvalOrFunc to be a pointer indirection too.

Joe Perches committed 12 years ago

Commit 24358801 parent 91f72e91 file · +34 −2Browse files

Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+34−2
@@ -289,11 +289,12 @@ our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; · 288 unmodified lines
289289 our $Binary = qr{(?i)0b[01]+$Int_type?};
290290 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
291291 our $Int = qr{[0-9]+$Int_type?};
292+our $Octal = qr{0[0-7]+$Int_type?};
292293 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
293294 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
294295 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
295296 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
296−our $Constant = qr{$Float|$Binary|$Hex|$Int};
297+our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
297298 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
298299 our $Compare = qr{<=|>=|==|!=|<|>};
299300 our $Arithmetic = qr{\+|-|\*|\/|%};
@@ -378,6 +379,15 @@ our @modifierList = ( · 78 unmodified lines
378379 qr{fastcall},
379380 );
380381
382+our @mode_permission_funcs = (
383+ ["module_param", 3],
384+ ["module_param_(?:array|named|string)", 4],
385+ ["module_param_array_named", 5],
386+ ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
387+ ["proc_create(?:_data|)", 2],
388+ ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
389+);
390+
381391 our $allowed_asm_includes = qr{(?x:
382392 irq|
383393 memory
@@ -423,7 +433,7 @@ our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; · 39 unmodified lines
423433 # Any use must be runtime checked with $^V
424434
425435 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
426−our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
436+our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
427437 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
428438
429439 sub deparenthesize {
@@ -4473,6 +4483,28 @@ sub process { · 4043 unmodified lines
44734483 WARN("EXPORTED_WORLD_WRITABLE",
44744484 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
44754485 }
4486+
4487+ foreach my $entry (@mode_permission_funcs) {
4488+ my $func = $entry->[0];
4489+ my $arg_pos = $entry->[1];
4490+
4491+ my $skip_args = "";
4492+ if ($arg_pos > 1) {
4493+ $arg_pos--;
4494+ $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
4495+ }
4496+ my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
4497+ if ($^V && $^V ge 5.10.0 &&
4498+ $line =~ /$test/) {
4499+ my $val = $1;
4500+ $val = $6 if ($skip_args ne "");
4501+
4502+ if ($val =~ /^$Int$/ && $val !~ /^$Octal$/) {
4503+ ERROR("NON_OCTAL_PERMISSIONS",
4504+ "Use octal not decimal permissions\n" . $herecurr);
4505+ }
4506+ }
4507+ }
44764508 }
44774509
44784510 # If we have no input at all, then there is nothing to report on