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 (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
289our $Binary = qr{(?i)0b[01]+$Int_type?};289our $Binary = qr{(?i)0b[01]+$Int_type?};
290our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};290our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
291our $Int = qr{[0-9]+$Int_type?};291our $Int = qr{[0-9]+$Int_type?};
292our $Octal = qr{0[0-7]+$Int_type?};
292our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};293our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
293our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};294our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
294our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};295our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
295our $Float = qr{$Float_hex|$Float_dec|$Float_int};296our $Float = qr{$Float_hex|$Float_dec|$Float_int};
296our $Constant = qr{$Float|$Binary|$Hex|$Int};297our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
297our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};298our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
298our $Compare = qr{<=|>=|==|!=|<|>};299our $Compare = qr{<=|>=|==|!=|<|>};
299our $Arithmetic = qr{\+|-|\*|\/|%};300our $Arithmetic = qr{\+|-|\*|\/|%};
@@ -378,6 +379,15 @@ our @modifierList = ( · 78 unmodified lines
378 qr{fastcall},379 qr{fastcall},
379);380);
380 381
382our @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
381our $allowed_asm_includes = qr{(?x:391our $allowed_asm_includes = qr{(?x:
382 irq|392 irq|
383 memory393 memory
@@ -423,7 +433,7 @@ our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; · 39 unmodified lines
423# Any use must be runtime checked with $^V433# Any use must be runtime checked with $^V
424 434
425our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;435our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
426our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};436our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
427our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};437our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
428 438
429sub deparenthesize {439sub deparenthesize {
@@ -4473,6 +4483,28 @@ sub process { · 4043 unmodified lines
4473 WARN("EXPORTED_WORLD_WRITABLE",4483 WARN("EXPORTED_WORLD_WRITABLE",
4474 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);4484 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
4475 }4485 }
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 }
4476 }4508 }
4477 4509
4478 # If we have no input at all, then there is nothing to report on4510 # If we have no input at all, then there is nothing to report on