50
50
my %warnings_extended = (
51
51
' COPYRIGHTYEAR' => ' copyright year incorrect' ,
52
52
' STRERROR' , => ' strerror() detected' ,
53
+ ' STRNCPY' , => ' strncpy() detected' ,
53
54
' STDERR' , => ' stderr detected' ,
54
55
);
55
56
56
57
my %warnings = (
57
58
' ASSIGNWITHINCONDITION' => ' assignment within conditional expression' ,
58
- ' ASTERISKNOSPACE' => ' pointer declared without space before asterisk' ,
59
- ' ASTERISKSPACE' => ' pointer declared with space after asterisk' ,
60
- ' BADCOMMAND' => ' bad !checksrc! instruction' ,
61
- ' BANNEDFUNC' => ' a banned function was used' ,
62
- ' BANNEDPREPROC' => ' a banned symbol was used on a preprocessor line' ,
63
- ' BRACEELSE' => ' } else on the same line' ,
64
- ' BRACEPOS' => ' wrong position for an open brace' ,
65
- ' BRACEWHILE' => ' A single space between open brace and while' ,
66
- ' COMMANOSPACE' => ' comma without following space' ,
67
- ' COMMENTNOSPACEEND' => ' no space before */' ,
68
- ' COMMENTNOSPACESTART' => ' no space following /*' ,
69
- ' COPYRIGHT' => ' file missing a copyright statement' ,
70
- ' CPPCOMMENTS' => ' // comment detected' ,
71
- ' DOBRACE' => ' A single space between do and open brace' ,
72
- ' EMPTYLINEBRACE' => ' Empty line before the open brace' ,
73
- ' EQUALSNOSPACE' => ' equals sign without following space' ,
74
- ' EQUALSNULL' => ' if/while comparison with == NULL' ,
75
- ' EXCLAMATIONSPACE' => ' Whitespace after exclamation mark in expression' ,
76
- ' FOPENMODE' => ' fopen needs a macro for the mode string' ,
77
- ' INCLUDEDUP' , => ' same file is included again' ,
78
- ' INDENTATION' => ' wrong start column for code' ,
79
- ' LONGLINE' => " Line longer than $max_column " ,
80
- ' SPACEBEFORELABEL' => ' labels not at the start of the line' ,
81
- ' MULTISPACE' => ' multiple spaces used when not suitable' ,
82
- ' NOSPACEEQUALS' => ' equals sign without preceding space' ,
83
- ' NOTEQUALSZERO' , => ' if/while comparison with != 0' ,
84
- ' ONELINECONDITION' => ' conditional block on the same line as the if()' ,
85
- ' OPENCOMMENT' => ' file ended with a /* comment still "open"' ,
86
- ' PARENBRACE' => ' ){ without sufficient space' ,
87
- ' RETURNNOSPACE' => ' return without space' ,
88
- ' SEMINOSPACE' => ' semicolon without following space' ,
89
- ' SIZEOFNOPAREN' => ' use of sizeof without parentheses' ,
90
- ' SNPRINTF' => ' use of snprintf' ,
91
- ' SPACEAFTERPAREN' => ' space after open parenthesis' ,
92
- ' SPACEBEFORECLOSE' => ' space before a close parenthesis' ,
93
- ' SPACEBEFORECOMMA' => ' space before a comma' ,
94
- ' SPACEBEFOREPAREN' => ' space before an open parenthesis' ,
95
- ' SPACESEMICOLON' => ' space before semicolon' ,
96
- ' SPACESWITCHCOLON' => ' space before colon of switch label' ,
97
- ' TABS' => ' TAB characters not allowed' ,
98
- ' TRAILINGSPACE' => ' Trailing whitespace on the line' ,
99
- ' TYPEDEFSTRUCT' => ' typedefed struct' ,
100
- ' UNUSEDIGNORE' => ' a warning ignore was not used' ,
59
+ ' ASTERISKNOSPACE' => ' pointer declared without space before asterisk' ,
60
+ ' ASTERISKSPACE' => ' pointer declared with space after asterisk' ,
61
+ ' BADCOMMAND' => ' bad !checksrc! instruction' ,
62
+ ' BANNEDFUNC' => ' a banned function was used' ,
63
+ ' BANNEDPREPROC' => ' a banned symbol was used on a preprocessor line' ,
64
+ ' BRACEELSE' => ' } else on the same line' ,
65
+ ' BRACEPOS' => ' wrong position for an open brace' ,
66
+ ' BRACEWHILE' => ' A single space between open brace and while' ,
67
+ ' COMMANOSPACE' => ' comma without following space' ,
68
+ ' COMMENTNOSPACEEND' => ' no space before */' ,
69
+ ' COMMENTNOSPACESTART' => ' no space following /*' ,
70
+ ' COPYRIGHT' => ' file missing a copyright statement' ,
71
+ ' CPPCOMMENTS' => ' // comment detected' ,
72
+ ' DOBRACE' => ' A single space between do and open brace' ,
73
+ ' EMPTYLINEBRACE' => ' Empty line before the open brace' ,
74
+ ' EQUALSNOSPACE' => ' equals sign without following space' ,
75
+ ' EQUALSNULL' => ' if/while comparison with == NULL' ,
76
+ ' EXCLAMATIONSPACE' => ' Whitespace after exclamation mark in expression' ,
77
+ ' FOPENMODE' => ' fopen needs a macro for the mode string' ,
78
+ ' INCLUDEDUP' , => ' same file is included again' ,
79
+ ' INDENTATION' => ' wrong start column for code' ,
80
+ ' LONGLINE' => " Line longer than $max_column " ,
81
+ ' SPACEBEFORELABEL' => ' labels not at the start of the line' ,
82
+ ' MULTISPACE' => ' multiple spaces used when not suitable' ,
83
+ ' NOSPACEC' => ' missing space around ternary colon operator' ,
84
+ ' NOSPACEEQUALS' => ' equals sign without preceding space' ,
85
+ ' NOSPACEQ' => ' missing space around ternary question mark operator' ,
86
+ ' NOSPACETHAN' => ' missing space around less or greater than' ,
87
+ ' NOTEQUALSZERO' , => ' if/while comparison with != 0' ,
88
+ ' ONELINECONDITION' => ' conditional block on the same line as the if()' ,
89
+ ' OPENCOMMENT' => ' file ended with a /* comment still "open"' ,
90
+ ' PARENBRACE' => ' ){ without sufficient space' ,
91
+ ' RETURNNOSPACE' => ' return without space' ,
92
+ ' SEMINOSPACE' => ' semicolon without following space' ,
93
+ ' SIZEOFNOPAREN' => ' use of sizeof without parentheses' ,
94
+ ' SNPRINTF' => ' use of snprintf' ,
95
+ ' SPACEAFTERPAREN' => ' space after open parenthesis' ,
96
+ ' SPACEBEFORECLOSE' => ' space before a close parenthesis' ,
97
+ ' SPACEBEFORECOMMA' => ' space before a comma' ,
98
+ ' SPACEBEFOREPAREN' => ' space before an open parenthesis' ,
99
+ ' SPACESEMICOLON' => ' space before semicolon' ,
100
+ ' SPACESWITCHCOLON' => ' space before colon of switch label' ,
101
+ ' TABS' => ' TAB characters not allowed' ,
102
+ ' TRAILINGSPACE' => ' Trailing whitespace on the line' ,
103
+ ' TYPEDEFSTRUCT' => ' typedefed struct' ,
104
+ ' UNUSEDIGNORE' => ' a warning ignore was not used' ,
101
105
);
102
106
103
107
sub readskiplist {
@@ -115,11 +119,23 @@ sub readskiplist {
115
119
# and since that's already handled via !checksrc! commands there is probably
116
120
# little use to add it.
117
121
sub readlocalfile {
122
+ my ($file ) = @_ ;
118
123
my $i = 0;
124
+ my $rcfile ;
119
125
120
- open (my $rcfile , " <" , " $dir /.checksrc" ) or return ;
126
+ if (($dir eq " ." ) && $file =~ / \/ / ) {
127
+ my $ldir ;
128
+ if ($file =~ / (.*)\/ / ) {
129
+ $ldir = $1 ;
130
+ open ($rcfile , " <" , " $dir /$ldir /.checksrc" ) or return ;
131
+ }
132
+ }
133
+ else {
134
+ open ($rcfile , " <" , " $dir /.checksrc" ) or return ;
135
+ }
121
136
122
137
while (<$rcfile >) {
138
+ $windows_os ? $_ =~ s /\r ?\n $// : chomp ;
123
139
$i ++;
124
140
125
141
# Lines starting with '#' are considered comments
@@ -263,7 +279,7 @@ sub checkwarn {
263
279
}
264
280
265
281
readskiplist();
266
- readlocalfile();
282
+ readlocalfile($file );
267
283
268
284
do {
269
285
if (" $wlist " !~ / $file / ) {
@@ -403,6 +419,13 @@ sub scanfile {
403
419
checksrc($cmd , $line , $file , $l )
404
420
}
405
421
422
+ if ($l =~ / ^#line (\d +) \" ([^\" ]*)\" / ) {
423
+ # a #line instruction
424
+ $file = $2 ;
425
+ $line = $1 ;
426
+ next ;
427
+ }
428
+
406
429
# check for a copyright statement and save the years
407
430
if ($l =~ / \* +copyright .* (\d\d\d\d |)/i ) {
408
431
my $count = 0;
@@ -603,6 +626,62 @@ sub scanfile {
603
626
" space after open parenthesis" );
604
627
}
605
628
629
+ # check spaces before colon
630
+ if ($nostr =~ / ^(.*[^']\? [^'].*)(\w |\) |\] |')\: /i ) {
631
+ my $m = $1 ;
632
+ my $e = $nostr ;
633
+ $e =~ s / '(.)':'(.)'/ $1 :$2 / g ; # eliminate chars quotes that surround colon
634
+ $e =~ s / ':'// g ; # ignore these
635
+ if ($e =~ / ^(.*[^']\? [^'].*)(\w |\) |\] |')\: /i ) {
636
+ checkwarn(" NOSPACEC" ,
637
+ $line , length ($m )+1, $file , $l ,
638
+ " missing space before colon" );
639
+ }
640
+ }
641
+ # check spaces after colon
642
+ if ($nostr =~ / ^(.*[^'"]\? [^'"].*)\: (\w |\) |\] |')/i ) {
643
+ my $m = $1 ;
644
+ my $e = $nostr ;
645
+ $e =~ s / '(.)':'(.)'/ $1 :$2 / g ; # eliminate chars quotes that surround colon
646
+ $e =~ s / ':'// g ; # ignore these
647
+ if ($e =~ / ^(.*[^'"]\? [^'"].*)\: (\w |\) |\] |')/i ) {
648
+ checkwarn(" NOSPACEC" ,
649
+ $line , length ($m )+1, $file , $l ,
650
+ " missing space after colon" );
651
+ }
652
+ }
653
+
654
+ # check spaces before question mark
655
+ if ($nostr =~ / ^(.*)(\w |\) |\] |')\? /i ) {
656
+ my $m = $1 ;
657
+ my $e = $nostr ;
658
+ $e =~ s / '?'// g ; # ignore these
659
+ if ($e =~ / ^(.*)(\w |\) |\] |')\? /i ) {
660
+ checkwarn(" NOSPACEQ" ,
661
+ $line , length ($m )+1, $file , $l ,
662
+ " missing space before question mark" );
663
+ }
664
+ }
665
+ # check spaces after question mark
666
+ if ($nostr =~ / ^(.*)\?\w /i ) {
667
+ checkwarn(" NOSPACEQ" ,
668
+ $line , length ($1 )+1, $file , $l ,
669
+ " missing space after question mark" );
670
+ }
671
+
672
+ # check spaces before less or greater than
673
+ if ($nostr =~ / ^(.*)(\w |\) |\] )[<>]/ ) {
674
+ checkwarn(" NOSPACETHAN" ,
675
+ $line , length ($1 )+1, $file , $l ,
676
+ " missing space before less or greater than" );
677
+ }
678
+ # check spaces after less or greater than
679
+ if ($nostr =~ / ^(.*)[^-][<>](\w |\( |\[ )/ ) {
680
+ checkwarn(" NOSPACETHAN" ,
681
+ $line , length ($1 )+1, $file , $l ,
682
+ " missing space after less or greater than" );
683
+ }
684
+
606
685
# check spaces before close parentheses, unless it was a space or a
607
686
# close parenthesis!
608
687
if ($l =~ / (.*[^\) ]) \) / ) {
@@ -712,7 +791,8 @@ sub scanfile {
712
791
strtok|
713
792
v?sprintf|
714
793
(str|_mbs|_tcs|_wcs)n?cat|
715
- LoadLibrary(Ex)?(A|W)?)
794
+ LoadLibrary(Ex)?(A|W)?|
795
+ _?w?access)
716
796
\s *\(
717
797
/x ) {
718
798
checkwarn(" BANNEDFUNC" ,
@@ -731,6 +811,18 @@ sub scanfile {
731
811
}
732
812
}
733
813
}
814
+ if ($warnings {" STRNCPY" }) {
815
+ # scan for use of banned strncpy. This is not a BANNEDFUNC to
816
+ # allow for individual enable/disable of this warning.
817
+ if ($l =~ / ^(.*\W )(strncpy)\s *\( /x ) {
818
+ if ($1 !~ / ^ *\# / ) {
819
+ # skip preprocessor lines
820
+ checkwarn(" STRNCPY" ,
821
+ $line , length ($1 ), $file , $ol ,
822
+ " use of $2 is banned" );
823
+ }
824
+ }
825
+ }
734
826
if ($warnings {" STDERR" }) {
735
827
# scan for use of banned stderr. This is not a BANNEDFUNC to
736
828
# allow for individual enable/disable of this warning.
@@ -915,7 +1007,7 @@ sub scanfile {
915
1007
checkwarn(" COPYRIGHT" , 1, 0, $file , " " , " Missing copyright statement" , 1);
916
1008
}
917
1009
918
- # COPYRIGHTYEAR is a extended warning so we must first see if it has been
1010
+ # COPYRIGHTYEAR is an extended warning so we must first see if it has been
919
1011
# enabled in .checksrc
920
1012
if (defined ($warnings {" COPYRIGHTYEAR" })) {
921
1013
# The check for updated copyrightyear is overly complicated in order to
0 commit comments