checksrc.pl 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 2011 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. # SPDX-License-Identifier: curl
  23. #
  24. ###########################################################################
  25. use strict;
  26. use warnings;
  27. my $max_column = 79;
  28. my $indent = 2;
  29. my $warnings = 0;
  30. my $swarnings = 0;
  31. my $errors = 0;
  32. my $serrors = 0;
  33. my $suppressed; # skipped problems
  34. my $file;
  35. my $dir=".";
  36. my $wlist="";
  37. my @alist;
  38. my $windows_os = $^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'msys';
  39. my $verbose;
  40. my %skiplist;
  41. my %ignore;
  42. my %ignore_set;
  43. my %ignore_used;
  44. my @ignore_line;
  45. my %warnings_extended = (
  46. 'COPYRIGHTYEAR' => 'copyright year incorrect',
  47. 'STRERROR', => 'strerror() detected',
  48. );
  49. my %warnings = (
  50. 'LONGLINE' => "Line longer than $max_column",
  51. 'TABS' => 'TAB characters not allowed',
  52. 'TRAILINGSPACE' => 'Trailing whitespace on the line',
  53. 'CPPCOMMENTS' => '// comment detected',
  54. 'SPACEBEFOREPAREN' => 'space before an open parenthesis',
  55. 'SPACEAFTERPAREN' => 'space after open parenthesis',
  56. 'SPACEBEFORECLOSE' => 'space before a close parenthesis',
  57. 'SPACEBEFORECOMMA' => 'space before a comma',
  58. 'RETURNNOSPACE' => 'return without space',
  59. 'COMMANOSPACE' => 'comma without following space',
  60. 'BRACEELSE' => '} else on the same line',
  61. 'PARENBRACE' => '){ without sufficient space',
  62. 'SPACESEMICOLON' => 'space before semicolon',
  63. 'BANNEDFUNC' => 'a banned function was used',
  64. 'FOPENMODE' => 'fopen needs a macro for the mode string',
  65. 'BRACEPOS' => 'wrong position for an open brace',
  66. 'INDENTATION' => 'wrong start column for code',
  67. 'COPYRIGHT' => 'file missing a copyright statement',
  68. 'BADCOMMAND' => 'bad !checksrc! instruction',
  69. 'UNUSEDIGNORE' => 'a warning ignore was not used',
  70. 'OPENCOMMENT' => 'file ended with a /* comment still "open"',
  71. 'ASTERISKSPACE' => 'pointer declared with space after asterisk',
  72. 'ASTERISKNOSPACE' => 'pointer declared without space before asterisk',
  73. 'ASSIGNWITHINCONDITION' => 'assignment within conditional expression',
  74. 'EQUALSNOSPACE' => 'equals sign without following space',
  75. 'NOSPACEEQUALS' => 'equals sign without preceding space',
  76. 'SEMINOSPACE' => 'semicolon without following space',
  77. 'MULTISPACE' => 'multiple spaces used when not suitable',
  78. 'SIZEOFNOPAREN' => 'use of sizeof without parentheses',
  79. 'SNPRINTF' => 'use of snprintf',
  80. 'ONELINECONDITION' => 'conditional block on the same line as the if()',
  81. 'TYPEDEFSTRUCT' => 'typedefed struct',
  82. 'DOBRACE' => 'A single space between do and open brace',
  83. 'BRACEWHILE' => 'A single space between open brace and while',
  84. 'EXCLAMATIONSPACE' => 'Whitespace after exclamation mark in expression',
  85. 'EMPTYLINEBRACE' => 'Empty line before the open brace',
  86. 'EQUALSNULL' => 'if/while comparison with == NULL',
  87. 'NOTEQUALSZERO', => 'if/while comparison with != 0',
  88. );
  89. sub readskiplist {
  90. open(W, "<$dir/checksrc.skip") or return;
  91. my @all=<W>;
  92. for(@all) {
  93. $windows_os ? $_ =~ s/\r?\n$// : chomp;
  94. $skiplist{$_}=1;
  95. }
  96. close(W);
  97. }
  98. # Reads the .checksrc in $dir for any extended warnings to enable locally.
  99. # Currently there is no support for disabling warnings from the standard set,
  100. # and since that's already handled via !checksrc! commands there is probably
  101. # little use to add it.
  102. sub readlocalfile {
  103. my $i = 0;
  104. open(my $rcfile, "<", "$dir/.checksrc") or return;
  105. while(<$rcfile>) {
  106. $i++;
  107. # Lines starting with '#' are considered comments
  108. if (/^\s*(#.*)/) {
  109. next;
  110. }
  111. elsif (/^\s*enable ([A-Z]+)$/) {
  112. if(!defined($warnings_extended{$1})) {
  113. print STDERR "invalid warning specified in .checksrc: \"$1\"\n";
  114. next;
  115. }
  116. $warnings{$1} = $warnings_extended{$1};
  117. }
  118. elsif (/^\s*disable ([A-Z]+)$/) {
  119. if(!defined($warnings{$1})) {
  120. print STDERR "invalid warning specified in .checksrc: \"$1\"\n";
  121. next;
  122. }
  123. # Accept-list
  124. push @alist, $1;
  125. }
  126. else {
  127. die "Invalid format in $dir/.checksrc on line $i\n";
  128. }
  129. }
  130. close($rcfile);
  131. }
  132. sub checkwarn {
  133. my ($name, $num, $col, $file, $line, $msg, $error) = @_;
  134. my $w=$error?"error":"warning";
  135. my $nowarn=0;
  136. #if(!$warnings{$name}) {
  137. # print STDERR "Dev! there's no description for $name!\n";
  138. #}
  139. # checksrc.skip
  140. if($skiplist{$line}) {
  141. $nowarn = 1;
  142. }
  143. # !checksrc! controlled
  144. elsif($ignore{$name}) {
  145. $ignore{$name}--;
  146. $ignore_used{$name}++;
  147. $nowarn = 1;
  148. if(!$ignore{$name}) {
  149. # reached zero, enable again
  150. enable_warn($name, $num, $file, $line);
  151. }
  152. }
  153. if($nowarn) {
  154. $suppressed++;
  155. if($w) {
  156. $swarnings++;
  157. }
  158. else {
  159. $serrors++;
  160. }
  161. return;
  162. }
  163. if($w) {
  164. $warnings++;
  165. }
  166. else {
  167. $errors++;
  168. }
  169. $col++;
  170. print "$file:$num:$col: $w: $msg ($name)\n";
  171. print " $line\n";
  172. if($col < 80) {
  173. my $pref = (' ' x $col);
  174. print "${pref}^\n";
  175. }
  176. }
  177. $file = shift @ARGV;
  178. while(defined $file) {
  179. if($file =~ /-D(.*)/) {
  180. $dir = $1;
  181. $file = shift @ARGV;
  182. next;
  183. }
  184. elsif($file =~ /-W(.*)/) {
  185. $wlist .= " $1 ";
  186. $file = shift @ARGV;
  187. next;
  188. }
  189. elsif($file =~ /-A(.+)/) {
  190. push @alist, $1;
  191. $file = shift @ARGV;
  192. next;
  193. }
  194. elsif($file =~ /-i([1-9])/) {
  195. $indent = $1 + 0;
  196. $file = shift @ARGV;
  197. next;
  198. }
  199. elsif($file =~ /-m([0-9]+)/) {
  200. $max_column = $1 + 0;
  201. $file = shift @ARGV;
  202. next;
  203. }
  204. elsif($file =~ /^(-h|--help)/) {
  205. undef $file;
  206. last;
  207. }
  208. last;
  209. }
  210. if(!$file) {
  211. print "checksrc.pl [option] <file1> [file2] ...\n";
  212. print " Options:\n";
  213. print " -A[rule] Accept this violation, can be used multiple times\n";
  214. print " -D[DIR] Directory to prepend file names\n";
  215. print " -h Show help output\n";
  216. print " -W[file] Skip the given file - ignore all its flaws\n";
  217. print " -i<n> Indent spaces. Default: 2\n";
  218. print " -m<n> Maximum line length. Default: 79\n";
  219. print "\nDetects and warns for these problems:\n";
  220. my @allw = keys %warnings;
  221. push @allw, keys %warnings_extended;
  222. for my $w (sort @allw) {
  223. if($warnings{$w}) {
  224. printf (" %-18s: %s\n", $w, $warnings{$w});
  225. }
  226. else {
  227. printf (" %-18s: %s[*]\n", $w, $warnings_extended{$w});
  228. }
  229. }
  230. print " [*] = disabled by default\n";
  231. exit;
  232. }
  233. readskiplist();
  234. readlocalfile();
  235. do {
  236. if("$wlist" !~ / $file /) {
  237. my $fullname = $file;
  238. $fullname = "$dir/$file" if ($fullname !~ '^\.?\.?/');
  239. scanfile($fullname);
  240. }
  241. $file = shift @ARGV;
  242. } while($file);
  243. sub accept_violations {
  244. for my $r (@alist) {
  245. if(!$warnings{$r}) {
  246. print "'$r' is not a warning to accept!\n";
  247. exit;
  248. }
  249. $ignore{$r}=999999;
  250. $ignore_used{$r}=0;
  251. }
  252. }
  253. sub checksrc_clear {
  254. undef %ignore;
  255. undef %ignore_set;
  256. undef @ignore_line;
  257. }
  258. sub checksrc_endoffile {
  259. my ($file) = @_;
  260. for(keys %ignore_set) {
  261. if($ignore_set{$_} && !$ignore_used{$_}) {
  262. checkwarn("UNUSEDIGNORE", $ignore_set{$_},
  263. length($_)+11, $file,
  264. $ignore_line[$ignore_set{$_}],
  265. "Unused ignore: $_");
  266. }
  267. }
  268. }
  269. sub enable_warn {
  270. my ($what, $line, $file, $l) = @_;
  271. # switch it back on, but warn if not triggered!
  272. if(!$ignore_used{$what}) {
  273. checkwarn("UNUSEDIGNORE",
  274. $line, length($what) + 11, $file, $l,
  275. "No warning was inhibited!");
  276. }
  277. $ignore_set{$what}=0;
  278. $ignore_used{$what}=0;
  279. $ignore{$what}=0;
  280. }
  281. sub checksrc {
  282. my ($cmd, $line, $file, $l) = @_;
  283. if($cmd =~ / *([^ ]*) *(.*)/) {
  284. my ($enable, $what) = ($1, $2);
  285. $what =~ s: *\*/$::; # cut off end of C comment
  286. # print "ENABLE $enable WHAT $what\n";
  287. if($enable eq "disable") {
  288. my ($warn, $scope)=($1, $2);
  289. if($what =~ /([^ ]*) +(.*)/) {
  290. ($warn, $scope)=($1, $2);
  291. }
  292. else {
  293. $warn = $what;
  294. $scope = 1;
  295. }
  296. # print "IGNORE $warn for SCOPE $scope\n";
  297. if($scope eq "all") {
  298. $scope=999999;
  299. }
  300. # Comparing for a literal zero rather than the scalar value zero
  301. # covers the case where $scope contains the ending '*' from the
  302. # comment. If we use a scalar comparison (==) we induce warnings
  303. # on non-scalar contents.
  304. if($scope eq "0") {
  305. checkwarn("BADCOMMAND",
  306. $line, 0, $file, $l,
  307. "Disable zero not supported, did you mean to enable?");
  308. }
  309. elsif($ignore_set{$warn}) {
  310. checkwarn("BADCOMMAND",
  311. $line, 0, $file, $l,
  312. "$warn already disabled from line $ignore_set{$warn}");
  313. }
  314. else {
  315. $ignore{$warn}=$scope;
  316. $ignore_set{$warn}=$line;
  317. $ignore_line[$line]=$l;
  318. }
  319. }
  320. elsif($enable eq "enable") {
  321. enable_warn($what, $line, $file, $l);
  322. }
  323. else {
  324. checkwarn("BADCOMMAND",
  325. $line, 0, $file, $l,
  326. "Illegal !checksrc! command");
  327. }
  328. }
  329. }
  330. sub nostrings {
  331. my ($str) = @_;
  332. $str =~ s/\".*\"//g;
  333. return $str;
  334. }
  335. sub scanfile {
  336. my ($file) = @_;
  337. my $line = 1;
  338. my $prevl="";
  339. my $prevpl="";
  340. my $l = "";
  341. my $prep = 0;
  342. my $prevp = 0;
  343. open(R, "<$file") || die "failed to open $file";
  344. my $incomment=0;
  345. my @copyright=();
  346. my %includes;
  347. checksrc_clear(); # for file based ignores
  348. accept_violations();
  349. while(<R>) {
  350. $windows_os ? $_ =~ s/\r?\n$// : chomp;
  351. my $l = $_;
  352. my $ol = $l; # keep the unmodified line for error reporting
  353. my $column = 0;
  354. # check for !checksrc! commands
  355. if($l =~ /\!checksrc\! (.*)/) {
  356. my $cmd = $1;
  357. checksrc($cmd, $line, $file, $l)
  358. }
  359. # check for a copyright statement and save the years
  360. if($l =~ /\* +copyright .* \d\d\d\d/i) {
  361. while($l =~ /([\d]{4})/g) {
  362. push @copyright, {
  363. year => $1,
  364. line => $line,
  365. col => index($l, $1),
  366. code => $l
  367. };
  368. }
  369. }
  370. # detect long lines
  371. if(length($l) > $max_column) {
  372. checkwarn("LONGLINE", $line, length($l), $file, $l,
  373. "Longer than $max_column columns");
  374. }
  375. # detect TAB characters
  376. if($l =~ /^(.*)\t/) {
  377. checkwarn("TABS",
  378. $line, length($1), $file, $l, "Contains TAB character", 1);
  379. }
  380. # detect trailing whitespace
  381. if($l =~ /^(.*)[ \t]+\z/) {
  382. checkwarn("TRAILINGSPACE",
  383. $line, length($1), $file, $l, "Trailing whitespace");
  384. }
  385. # ------------------------------------------------------------
  386. # Above this marker, the checks were done on lines *including*
  387. # comments
  388. # ------------------------------------------------------------
  389. # strip off C89 comments
  390. comment:
  391. if(!$incomment) {
  392. if($l =~ s/\/\*.*\*\// /g) {
  393. # full /* comments */ were removed!
  394. }
  395. if($l =~ s/\/\*.*//) {
  396. # start of /* comment was removed
  397. $incomment = 1;
  398. }
  399. }
  400. else {
  401. if($l =~ s/.*\*\///) {
  402. # end of comment */ was removed
  403. $incomment = 0;
  404. goto comment;
  405. }
  406. else {
  407. # still within a comment
  408. $l="";
  409. }
  410. }
  411. # ------------------------------------------------------------
  412. # Below this marker, the checks were done on lines *without*
  413. # comments
  414. # ------------------------------------------------------------
  415. # prev line was a preprocessor **and** ended with a backslash
  416. if($prep && ($prevpl =~ /\\ *\z/)) {
  417. # this is still a preprocessor line
  418. $prep = 1;
  419. goto preproc;
  420. }
  421. $prep = 0;
  422. # crude attempt to detect // comments without too many false
  423. # positives
  424. if($l =~ /^(([^"\*]*)[^:"]|)\/\//) {
  425. checkwarn("CPPCOMMENTS",
  426. $line, length($1), $file, $l, "\/\/ comment");
  427. }
  428. if($l =~ /^(\#\s*include\s+)([\">].*[>}"])/) {
  429. my ($pre, $path) = ($1, $2);
  430. if($includes{$path}) {
  431. checkwarn("INCLUDEDUP",
  432. $line, length($1), $file, $l, "duplicated include");
  433. }
  434. $includes{$path} = $l;
  435. }
  436. # detect and strip preprocessor directives
  437. if($l =~ /^[ \t]*\#/) {
  438. # preprocessor line
  439. $prep = 1;
  440. goto preproc;
  441. }
  442. my $nostr = nostrings($l);
  443. # check spaces after for/if/while/function call
  444. if($nostr =~ /^(.*)(for|if|while| ([a-zA-Z0-9_]+)) \((.)/) {
  445. if($1 =~ / *\#/) {
  446. # this is a #if, treat it differently
  447. }
  448. elsif(defined $3 && $3 eq "return") {
  449. # return must have a space
  450. }
  451. elsif(defined $3 && $3 eq "case") {
  452. # case must have a space
  453. }
  454. elsif($4 eq "*") {
  455. # (* beginning makes the space OK!
  456. }
  457. elsif($1 =~ / *typedef/) {
  458. # typedefs can use space-paren
  459. }
  460. else {
  461. checkwarn("SPACEBEFOREPAREN", $line, length($1)+length($2), $file, $l,
  462. "$2 with space");
  463. }
  464. }
  465. # check for '== NULL' in if/while conditions but not if the thing on
  466. # the left of it is a function call
  467. if($nostr =~ /^(.*)(if|while)(\(.*?)([!=]= NULL|NULL [!=]=)/) {
  468. checkwarn("EQUALSNULL", $line,
  469. length($1) + length($2) + length($3),
  470. $file, $l, "we prefer !variable instead of \"== NULL\" comparisons");
  471. }
  472. # check for '!= 0' in if/while conditions but not if the thing on
  473. # the left of it is a function call
  474. if($nostr =~ /^(.*)(if|while)(\(.*[^)]) != 0[^x]/) {
  475. checkwarn("NOTEQUALSZERO", $line,
  476. length($1) + length($2) + length($3),
  477. $file, $l, "we prefer if(rc) instead of \"rc != 0\" comparisons");
  478. }
  479. # check spaces in 'do {'
  480. if($nostr =~ /^( *)do( *)\{/ && length($2) != 1) {
  481. checkwarn("DOBRACE", $line, length($1) + 2, $file, $l, "one space after do before brace");
  482. }
  483. # check spaces in 'do {'
  484. elsif($nostr =~ /^( *)\}( *)while/ && length($2) != 1) {
  485. checkwarn("BRACEWHILE", $line, length($1) + 2, $file, $l, "one space between brace and while");
  486. }
  487. if($nostr =~ /^((.*\s)(if) *\()(.*)\)(.*)/) {
  488. my $pos = length($1);
  489. my $postparen = $5;
  490. my $cond = $4;
  491. if($cond =~ / = /) {
  492. checkwarn("ASSIGNWITHINCONDITION",
  493. $line, $pos+1, $file, $l,
  494. "assignment within conditional expression");
  495. }
  496. my $temp = $cond;
  497. $temp =~ s/\(//g; # remove open parens
  498. my $openc = length($cond) - length($temp);
  499. $temp = $cond;
  500. $temp =~ s/\)//g; # remove close parens
  501. my $closec = length($cond) - length($temp);
  502. my $even = $openc == $closec;
  503. if($l =~ / *\#/) {
  504. # this is a #if, treat it differently
  505. }
  506. elsif($even && $postparen &&
  507. ($postparen !~ /^ *$/) && ($postparen !~ /^ *[,{&|\\]+/)) {
  508. checkwarn("ONELINECONDITION",
  509. $line, length($l)-length($postparen), $file, $l,
  510. "conditional block on the same line");
  511. }
  512. }
  513. # check spaces after open parentheses
  514. if($l =~ /^(.*[a-z])\( /i) {
  515. checkwarn("SPACEAFTERPAREN",
  516. $line, length($1)+1, $file, $l,
  517. "space after open parenthesis");
  518. }
  519. # check spaces before close parentheses, unless it was a space or a
  520. # close parenthesis!
  521. if($l =~ /(.*[^\) ]) \)/) {
  522. checkwarn("SPACEBEFORECLOSE",
  523. $line, length($1)+1, $file, $l,
  524. "space before close parenthesis");
  525. }
  526. # check spaces before comma!
  527. if($l =~ /(.*[^ ]) ,/) {
  528. checkwarn("SPACEBEFORECOMMA",
  529. $line, length($1)+1, $file, $l,
  530. "space before comma");
  531. }
  532. # check for "return(" without space
  533. if($l =~ /^(.*)return\(/) {
  534. if($1 =~ / *\#/) {
  535. # this is a #if, treat it differently
  536. }
  537. else {
  538. checkwarn("RETURNNOSPACE", $line, length($1)+6, $file, $l,
  539. "return without space before paren");
  540. }
  541. }
  542. # check for "sizeof" without parenthesis
  543. if(($l =~ /^(.*)sizeof *([ (])/) && ($2 ne "(")) {
  544. if($1 =~ / *\#/) {
  545. # this is a #if, treat it differently
  546. }
  547. else {
  548. checkwarn("SIZEOFNOPAREN", $line, length($1)+6, $file, $l,
  549. "sizeof without parenthesis");
  550. }
  551. }
  552. # check for comma without space
  553. if($l =~ /^(.*),[^ \n]/) {
  554. my $pref=$1;
  555. my $ign=0;
  556. if($pref =~ / *\#/) {
  557. # this is a #if, treat it differently
  558. $ign=1;
  559. }
  560. elsif($pref =~ /\/\*/) {
  561. # this is a comment
  562. $ign=1;
  563. }
  564. elsif($pref =~ /[\"\']/) {
  565. $ign = 1;
  566. # There is a quote here, figure out whether the comma is
  567. # within a string or '' or not.
  568. if($pref =~ /\"/) {
  569. # within a string
  570. }
  571. elsif($pref =~ /\'$/) {
  572. # a single letter
  573. }
  574. else {
  575. $ign = 0;
  576. }
  577. }
  578. if(!$ign) {
  579. checkwarn("COMMANOSPACE", $line, length($pref)+1, $file, $l,
  580. "comma without following space");
  581. }
  582. }
  583. # check for "} else"
  584. if($l =~ /^(.*)\} *else/) {
  585. checkwarn("BRACEELSE",
  586. $line, length($1), $file, $l, "else after closing brace on same line");
  587. }
  588. # check for "){"
  589. if($l =~ /^(.*)\)\{/) {
  590. checkwarn("PARENBRACE",
  591. $line, length($1)+1, $file, $l, "missing space after close paren");
  592. }
  593. # check for "^{" with an empty line before it
  594. if(($l =~ /^\{/) && ($prevl =~ /^[ \t]*\z/)) {
  595. checkwarn("EMPTYLINEBRACE",
  596. $line, 0, $file, $l, "empty line before open brace");
  597. }
  598. # check for space before the semicolon last in a line
  599. if($l =~ /^(.*[^ ].*) ;$/) {
  600. checkwarn("SPACESEMICOLON",
  601. $line, length($1), $file, $ol, "no space before semicolon");
  602. }
  603. # scan for use of banned functions
  604. if($l =~ /^(.*\W)
  605. (gmtime|localtime|
  606. gets|
  607. strtok|
  608. v?sprintf|
  609. (str|_mbs|_tcs|_wcs)n?cat|
  610. LoadLibrary(Ex)?(A|W)?)
  611. \s*\(
  612. /x) {
  613. checkwarn("BANNEDFUNC",
  614. $line, length($1), $file, $ol,
  615. "use of $2 is banned");
  616. }
  617. if($warnings{"STRERROR"}) {
  618. # scan for use of banned strerror. This is not a BANNEDFUNC to
  619. # allow for individual enable/disable of this warning.
  620. if($l =~ /^(.*\W)(strerror)\s*\(/x) {
  621. if($1 !~ /^ *\#/) {
  622. # skip preprocessor lines
  623. checkwarn("STRERROR",
  624. $line, length($1), $file, $ol,
  625. "use of $2 is banned");
  626. }
  627. }
  628. }
  629. # scan for use of snprintf for curl-internals reasons
  630. if($l =~ /^(.*\W)(v?snprintf)\s*\(/x) {
  631. checkwarn("SNPRINTF",
  632. $line, length($1), $file, $ol,
  633. "use of $2 is banned");
  634. }
  635. # scan for use of non-binary fopen without the macro
  636. if($l =~ /^(.*\W)fopen\s*\([^,]*, *\"([^"]*)/) {
  637. my $mode = $2;
  638. if($mode !~ /b/) {
  639. checkwarn("FOPENMODE",
  640. $line, length($1), $file, $ol,
  641. "use of non-binary fopen without FOPEN_* macro: $mode");
  642. }
  643. }
  644. # check for open brace first on line but not first column only alert
  645. # if previous line ended with a close paren and it wasn't a cpp line
  646. if(($prevl =~ /\)\z/) && ($l =~ /^( +)\{/) && !$prevp) {
  647. checkwarn("BRACEPOS",
  648. $line, length($1), $file, $ol, "badly placed open brace");
  649. }
  650. # if the previous line starts with if/while/for AND ends with an open
  651. # brace, or an else statement, check that this line is indented $indent
  652. # more steps, if not a cpp line
  653. if(!$prevp && ($prevl =~ /^( *)((if|while|for)\(.*\{|else)\z/)) {
  654. my $first = length($1);
  655. # this line has some character besides spaces
  656. if($l =~ /^( *)[^ ]/) {
  657. my $second = length($1);
  658. my $expect = $first+$indent;
  659. if($expect != $second) {
  660. my $diff = $second - $first;
  661. checkwarn("INDENTATION", $line, length($1), $file, $ol,
  662. "not indented $indent steps (uses $diff)");
  663. }
  664. }
  665. }
  666. # check for 'char * name'
  667. if(($l =~ /(^.*(char|int|long|void|CURL|CURLM|CURLMsg|[cC]url_[A-Za-z_]+|struct [a-zA-Z_]+) *(\*+)) (\w+)/) && ($4 !~ /^(const|volatile)$/)) {
  668. checkwarn("ASTERISKSPACE",
  669. $line, length($1), $file, $ol,
  670. "space after declarative asterisk");
  671. }
  672. # check for 'char*'
  673. if(($l =~ /(^.*(char|int|long|void|curl_slist|CURL|CURLM|CURLMsg|curl_httppost|sockaddr_in|FILE)\*)/)) {
  674. checkwarn("ASTERISKNOSPACE",
  675. $line, length($1)-1, $file, $ol,
  676. "no space before asterisk");
  677. }
  678. # check for 'void func() {', but avoid false positives by requiring
  679. # both an open and closed parentheses before the open brace
  680. if($l =~ /^((\w).*)\{\z/) {
  681. my $k = $1;
  682. $k =~ s/const *//;
  683. $k =~ s/static *//;
  684. if($k =~ /\(.*\)/) {
  685. checkwarn("BRACEPOS",
  686. $line, length($l)-1, $file, $ol,
  687. "wrongly placed open brace");
  688. }
  689. }
  690. # check for equals sign without spaces next to it
  691. if($nostr =~ /(.*)\=[a-z0-9]/i) {
  692. checkwarn("EQUALSNOSPACE",
  693. $line, length($1)+1, $file, $ol,
  694. "no space after equals sign");
  695. }
  696. # check for equals sign without spaces before it
  697. elsif($nostr =~ /(.*)[a-z0-9]\=/i) {
  698. checkwarn("NOSPACEEQUALS",
  699. $line, length($1)+1, $file, $ol,
  700. "no space before equals sign");
  701. }
  702. # check for plus signs without spaces next to it
  703. if($nostr =~ /(.*)[^+]\+[a-z0-9]/i) {
  704. checkwarn("PLUSNOSPACE",
  705. $line, length($1)+1, $file, $ol,
  706. "no space after plus sign");
  707. }
  708. # check for plus sign without spaces before it
  709. elsif($nostr =~ /(.*)[a-z0-9]\+[^+]/i) {
  710. checkwarn("NOSPACEPLUS",
  711. $line, length($1)+1, $file, $ol,
  712. "no space before plus sign");
  713. }
  714. # check for semicolons without space next to it
  715. if($nostr =~ /(.*)\;[a-z0-9]/i) {
  716. checkwarn("SEMINOSPACE",
  717. $line, length($1)+1, $file, $ol,
  718. "no space after semicolon");
  719. }
  720. # typedef struct ... {
  721. if($nostr =~ /^(.*)typedef struct.*{/) {
  722. checkwarn("TYPEDEFSTRUCT",
  723. $line, length($1)+1, $file, $ol,
  724. "typedef'ed struct");
  725. }
  726. if($nostr =~ /(.*)! +(\w|\()/) {
  727. checkwarn("EXCLAMATIONSPACE",
  728. $line, length($1)+1, $file, $ol,
  729. "space after exclamation mark");
  730. }
  731. # check for more than one consecutive space before open brace or
  732. # question mark. Skip lines containing strings since they make it hard
  733. # due to artificially getting multiple spaces
  734. if(($l eq $nostr) &&
  735. $nostr =~ /^(.*(\S)) + [{?]/i) {
  736. checkwarn("MULTISPACE",
  737. $line, length($1)+1, $file, $ol,
  738. "multiple spaces");
  739. }
  740. preproc:
  741. $line++;
  742. $prevp = $prep;
  743. $prevl = $ol if(!$prep);
  744. $prevpl = $ol if($prep);
  745. }
  746. if(!scalar(@copyright)) {
  747. checkwarn("COPYRIGHT", 1, 0, $file, "", "Missing copyright statement", 1);
  748. }
  749. # COPYRIGHTYEAR is a extended warning so we must first see if it has been
  750. # enabled in .checksrc
  751. if(defined($warnings{"COPYRIGHTYEAR"})) {
  752. # The check for updated copyrightyear is overly complicated in order to
  753. # not punish current hacking for past sins. The copyright years are
  754. # right now a bit behind, so enforcing copyright year checking on all
  755. # files would cause hundreds of errors. Instead we only look at files
  756. # which are tracked in the Git repo and edited in the workdir, or
  757. # committed locally on the branch without being in upstream master.
  758. #
  759. # The simple and naive test is to simply check for the current year,
  760. # but updating the year even without an edit is against project policy
  761. # (and it would fail every file on January 1st).
  762. #
  763. # A rather more interesting, and correct, check would be to not test
  764. # only locally committed files but inspect all files wrt the year of
  765. # their last commit. Removing the `git rev-list origin/master..HEAD`
  766. # condition below will enforce copyright year checks against the year
  767. # the file was last committed (and thus edited to some degree).
  768. my $commityear = undef;
  769. @copyright = sort {$$b{year} cmp $$a{year}} @copyright;
  770. # if the file is modified, assume commit year this year
  771. if(`git status -s -- $file` =~ /^ [MARCU]/) {
  772. $commityear = (localtime(time))[5] + 1900;
  773. }
  774. else {
  775. # min-parents=1 to ignore wrong initial commit in truncated repos
  776. my $grl = `git rev-list --max-count=1 --min-parents=1 --timestamp HEAD -- $file`;
  777. if($grl) {
  778. chomp $grl;
  779. $commityear = (localtime((split(/ /, $grl))[0]))[5] + 1900;
  780. }
  781. }
  782. if(defined($commityear) && scalar(@copyright) &&
  783. $copyright[0]{year} != $commityear) {
  784. checkwarn("COPYRIGHTYEAR", $copyright[0]{line}, $copyright[0]{col},
  785. $file, $copyright[0]{code},
  786. "Copyright year out of date, should be $commityear, " .
  787. "is $copyright[0]{year}", 1);
  788. }
  789. }
  790. if($incomment) {
  791. checkwarn("OPENCOMMENT", 1, 0, $file, "", "Missing closing comment", 1);
  792. }
  793. checksrc_endoffile($file);
  794. close(R);
  795. }
  796. if($errors || $warnings || $verbose) {
  797. printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
  798. if($suppressed) {
  799. printf "checksrc: %d errors and %d warnings suppressed\n",
  800. $serrors,
  801. $swarnings;
  802. }
  803. exit 5; # return failure
  804. }