checksrc.pl 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. checksrc_clear(); # for file based ignores
  347. accept_violations();
  348. while(<R>) {
  349. $windows_os ? $_ =~ s/\r?\n$// : chomp;
  350. my $l = $_;
  351. my $ol = $l; # keep the unmodified line for error reporting
  352. my $column = 0;
  353. # check for !checksrc! commands
  354. if($l =~ /\!checksrc\! (.*)/) {
  355. my $cmd = $1;
  356. checksrc($cmd, $line, $file, $l)
  357. }
  358. # check for a copyright statement and save the years
  359. if($l =~ /\* +copyright .* \d\d\d\d/i) {
  360. while($l =~ /([\d]{4})/g) {
  361. push @copyright, {
  362. year => $1,
  363. line => $line,
  364. col => index($l, $1),
  365. code => $l
  366. };
  367. }
  368. }
  369. # detect long lines
  370. if(length($l) > $max_column) {
  371. checkwarn("LONGLINE", $line, length($l), $file, $l,
  372. "Longer than $max_column columns");
  373. }
  374. # detect TAB characters
  375. if($l =~ /^(.*)\t/) {
  376. checkwarn("TABS",
  377. $line, length($1), $file, $l, "Contains TAB character", 1);
  378. }
  379. # detect trailing whitespace
  380. if($l =~ /^(.*)[ \t]+\z/) {
  381. checkwarn("TRAILINGSPACE",
  382. $line, length($1), $file, $l, "Trailing whitespace");
  383. }
  384. # ------------------------------------------------------------
  385. # Above this marker, the checks were done on lines *including*
  386. # comments
  387. # ------------------------------------------------------------
  388. # strip off C89 comments
  389. comment:
  390. if(!$incomment) {
  391. if($l =~ s/\/\*.*\*\// /g) {
  392. # full /* comments */ were removed!
  393. }
  394. if($l =~ s/\/\*.*//) {
  395. # start of /* comment was removed
  396. $incomment = 1;
  397. }
  398. }
  399. else {
  400. if($l =~ s/.*\*\///) {
  401. # end of comment */ was removed
  402. $incomment = 0;
  403. goto comment;
  404. }
  405. else {
  406. # still within a comment
  407. $l="";
  408. }
  409. }
  410. # ------------------------------------------------------------
  411. # Below this marker, the checks were done on lines *without*
  412. # comments
  413. # ------------------------------------------------------------
  414. # prev line was a preprocessor **and** ended with a backslash
  415. if($prep && ($prevpl =~ /\\ *\z/)) {
  416. # this is still a preprocessor line
  417. $prep = 1;
  418. goto preproc;
  419. }
  420. $prep = 0;
  421. # crude attempt to detect // comments without too many false
  422. # positives
  423. if($l =~ /^(([^"\*]*)[^:"]|)\/\//) {
  424. checkwarn("CPPCOMMENTS",
  425. $line, length($1), $file, $l, "\/\/ comment");
  426. }
  427. # detect and strip preprocessor directives
  428. if($l =~ /^[ \t]*\#/) {
  429. # preprocessor line
  430. $prep = 1;
  431. goto preproc;
  432. }
  433. my $nostr = nostrings($l);
  434. # check spaces after for/if/while/function call
  435. if($nostr =~ /^(.*)(for|if|while| ([a-zA-Z0-9_]+)) \((.)/) {
  436. if($1 =~ / *\#/) {
  437. # this is a #if, treat it differently
  438. }
  439. elsif(defined $3 && $3 eq "return") {
  440. # return must have a space
  441. }
  442. elsif(defined $3 && $3 eq "case") {
  443. # case must have a space
  444. }
  445. elsif($4 eq "*") {
  446. # (* beginning makes the space OK!
  447. }
  448. elsif($1 =~ / *typedef/) {
  449. # typedefs can use space-paren
  450. }
  451. else {
  452. checkwarn("SPACEBEFOREPAREN", $line, length($1)+length($2), $file, $l,
  453. "$2 with space");
  454. }
  455. }
  456. # check for '== NULL' in if/while conditions but not if the thing on
  457. # the left of it is a function call
  458. if($nostr =~ /^(.*)(if|while)(\(.*?)([!=]= NULL|NULL [!=]=)/) {
  459. checkwarn("EQUALSNULL", $line,
  460. length($1) + length($2) + length($3),
  461. $file, $l, "we prefer !variable instead of \"== NULL\" comparisons");
  462. }
  463. # check for '!= 0' in if/while conditions but not if the thing on
  464. # the left of it is a function call
  465. if($nostr =~ /^(.*)(if|while)(\(.*[^)]) != 0[^x]/) {
  466. checkwarn("NOTEQUALSZERO", $line,
  467. length($1) + length($2) + length($3),
  468. $file, $l, "we prefer if(rc) instead of \"rc != 0\" comparisons");
  469. }
  470. # check spaces in 'do {'
  471. if($nostr =~ /^( *)do( *)\{/ && length($2) != 1) {
  472. checkwarn("DOBRACE", $line, length($1) + 2, $file, $l, "one space after do before brace");
  473. }
  474. # check spaces in 'do {'
  475. elsif($nostr =~ /^( *)\}( *)while/ && length($2) != 1) {
  476. checkwarn("BRACEWHILE", $line, length($1) + 2, $file, $l, "one space between brace and while");
  477. }
  478. if($nostr =~ /^((.*\s)(if) *\()(.*)\)(.*)/) {
  479. my $pos = length($1);
  480. my $postparen = $5;
  481. my $cond = $4;
  482. if($cond =~ / = /) {
  483. checkwarn("ASSIGNWITHINCONDITION",
  484. $line, $pos+1, $file, $l,
  485. "assignment within conditional expression");
  486. }
  487. my $temp = $cond;
  488. $temp =~ s/\(//g; # remove open parens
  489. my $openc = length($cond) - length($temp);
  490. $temp = $cond;
  491. $temp =~ s/\)//g; # remove close parens
  492. my $closec = length($cond) - length($temp);
  493. my $even = $openc == $closec;
  494. if($l =~ / *\#/) {
  495. # this is a #if, treat it differently
  496. }
  497. elsif($even && $postparen &&
  498. ($postparen !~ /^ *$/) && ($postparen !~ /^ *[,{&|\\]+/)) {
  499. checkwarn("ONELINECONDITION",
  500. $line, length($l)-length($postparen), $file, $l,
  501. "conditional block on the same line");
  502. }
  503. }
  504. # check spaces after open parentheses
  505. if($l =~ /^(.*[a-z])\( /i) {
  506. checkwarn("SPACEAFTERPAREN",
  507. $line, length($1)+1, $file, $l,
  508. "space after open parenthesis");
  509. }
  510. # check spaces before close parentheses, unless it was a space or a
  511. # close parenthesis!
  512. if($l =~ /(.*[^\) ]) \)/) {
  513. checkwarn("SPACEBEFORECLOSE",
  514. $line, length($1)+1, $file, $l,
  515. "space before close parenthesis");
  516. }
  517. # check spaces before comma!
  518. if($l =~ /(.*[^ ]) ,/) {
  519. checkwarn("SPACEBEFORECOMMA",
  520. $line, length($1)+1, $file, $l,
  521. "space before comma");
  522. }
  523. # check for "return(" without space
  524. if($l =~ /^(.*)return\(/) {
  525. if($1 =~ / *\#/) {
  526. # this is a #if, treat it differently
  527. }
  528. else {
  529. checkwarn("RETURNNOSPACE", $line, length($1)+6, $file, $l,
  530. "return without space before paren");
  531. }
  532. }
  533. # check for "sizeof" without parenthesis
  534. if(($l =~ /^(.*)sizeof *([ (])/) && ($2 ne "(")) {
  535. if($1 =~ / *\#/) {
  536. # this is a #if, treat it differently
  537. }
  538. else {
  539. checkwarn("SIZEOFNOPAREN", $line, length($1)+6, $file, $l,
  540. "sizeof without parenthesis");
  541. }
  542. }
  543. # check for comma without space
  544. if($l =~ /^(.*),[^ \n]/) {
  545. my $pref=$1;
  546. my $ign=0;
  547. if($pref =~ / *\#/) {
  548. # this is a #if, treat it differently
  549. $ign=1;
  550. }
  551. elsif($pref =~ /\/\*/) {
  552. # this is a comment
  553. $ign=1;
  554. }
  555. elsif($pref =~ /[\"\']/) {
  556. $ign = 1;
  557. # There is a quote here, figure out whether the comma is
  558. # within a string or '' or not.
  559. if($pref =~ /\"/) {
  560. # within a string
  561. }
  562. elsif($pref =~ /\'$/) {
  563. # a single letter
  564. }
  565. else {
  566. $ign = 0;
  567. }
  568. }
  569. if(!$ign) {
  570. checkwarn("COMMANOSPACE", $line, length($pref)+1, $file, $l,
  571. "comma without following space");
  572. }
  573. }
  574. # check for "} else"
  575. if($l =~ /^(.*)\} *else/) {
  576. checkwarn("BRACEELSE",
  577. $line, length($1), $file, $l, "else after closing brace on same line");
  578. }
  579. # check for "){"
  580. if($l =~ /^(.*)\)\{/) {
  581. checkwarn("PARENBRACE",
  582. $line, length($1)+1, $file, $l, "missing space after close paren");
  583. }
  584. # check for "^{" with an empty line before it
  585. if(($l =~ /^\{/) && ($prevl =~ /^[ \t]*\z/)) {
  586. checkwarn("EMPTYLINEBRACE",
  587. $line, 0, $file, $l, "empty line before open brace");
  588. }
  589. # check for space before the semicolon last in a line
  590. if($l =~ /^(.*[^ ].*) ;$/) {
  591. checkwarn("SPACESEMICOLON",
  592. $line, length($1), $file, $ol, "no space before semicolon");
  593. }
  594. # scan for use of banned functions
  595. if($l =~ /^(.*\W)
  596. (gmtime|localtime|
  597. gets|
  598. strtok|
  599. v?sprintf|
  600. (str|_mbs|_tcs|_wcs)n?cat|
  601. LoadLibrary(Ex)?(A|W)?)
  602. \s*\(
  603. /x) {
  604. checkwarn("BANNEDFUNC",
  605. $line, length($1), $file, $ol,
  606. "use of $2 is banned");
  607. }
  608. if($warnings{"STRERROR"}) {
  609. # scan for use of banned strerror. This is not a BANNEDFUNC to
  610. # allow for individual enable/disable of this warning.
  611. if($l =~ /^(.*\W)(strerror)\s*\(/x) {
  612. if($1 !~ /^ *\#/) {
  613. # skip preprocessor lines
  614. checkwarn("STRERROR",
  615. $line, length($1), $file, $ol,
  616. "use of $2 is banned");
  617. }
  618. }
  619. }
  620. # scan for use of snprintf for curl-internals reasons
  621. if($l =~ /^(.*\W)(v?snprintf)\s*\(/x) {
  622. checkwarn("SNPRINTF",
  623. $line, length($1), $file, $ol,
  624. "use of $2 is banned");
  625. }
  626. # scan for use of non-binary fopen without the macro
  627. if($l =~ /^(.*\W)fopen\s*\([^,]*, *\"([^"]*)/) {
  628. my $mode = $2;
  629. if($mode !~ /b/) {
  630. checkwarn("FOPENMODE",
  631. $line, length($1), $file, $ol,
  632. "use of non-binary fopen without FOPEN_* macro: $mode");
  633. }
  634. }
  635. # check for open brace first on line but not first column only alert
  636. # if previous line ended with a close paren and it wasn't a cpp line
  637. if(($prevl =~ /\)\z/) && ($l =~ /^( +)\{/) && !$prevp) {
  638. checkwarn("BRACEPOS",
  639. $line, length($1), $file, $ol, "badly placed open brace");
  640. }
  641. # if the previous line starts with if/while/for AND ends with an open
  642. # brace, or an else statement, check that this line is indented $indent
  643. # more steps, if not a cpp line
  644. if(!$prevp && ($prevl =~ /^( *)((if|while|for)\(.*\{|else)\z/)) {
  645. my $first = length($1);
  646. # this line has some character besides spaces
  647. if($l =~ /^( *)[^ ]/) {
  648. my $second = length($1);
  649. my $expect = $first+$indent;
  650. if($expect != $second) {
  651. my $diff = $second - $first;
  652. checkwarn("INDENTATION", $line, length($1), $file, $ol,
  653. "not indented $indent steps (uses $diff)");
  654. }
  655. }
  656. }
  657. # check for 'char * name'
  658. if(($l =~ /(^.*(char|int|long|void|CURL|CURLM|CURLMsg|[cC]url_[A-Za-z_]+|struct [a-zA-Z_]+) *(\*+)) (\w+)/) && ($4 !~ /^(const|volatile)$/)) {
  659. checkwarn("ASTERISKSPACE",
  660. $line, length($1), $file, $ol,
  661. "space after declarative asterisk");
  662. }
  663. # check for 'char*'
  664. if(($l =~ /(^.*(char|int|long|void|curl_slist|CURL|CURLM|CURLMsg|curl_httppost|sockaddr_in|FILE)\*)/)) {
  665. checkwarn("ASTERISKNOSPACE",
  666. $line, length($1)-1, $file, $ol,
  667. "no space before asterisk");
  668. }
  669. # check for 'void func() {', but avoid false positives by requiring
  670. # both an open and closed parentheses before the open brace
  671. if($l =~ /^((\w).*)\{\z/) {
  672. my $k = $1;
  673. $k =~ s/const *//;
  674. $k =~ s/static *//;
  675. if($k =~ /\(.*\)/) {
  676. checkwarn("BRACEPOS",
  677. $line, length($l)-1, $file, $ol,
  678. "wrongly placed open brace");
  679. }
  680. }
  681. # check for equals sign without spaces next to it
  682. if($nostr =~ /(.*)\=[a-z0-9]/i) {
  683. checkwarn("EQUALSNOSPACE",
  684. $line, length($1)+1, $file, $ol,
  685. "no space after equals sign");
  686. }
  687. # check for equals sign without spaces before it
  688. elsif($nostr =~ /(.*)[a-z0-9]\=/i) {
  689. checkwarn("NOSPACEEQUALS",
  690. $line, length($1)+1, $file, $ol,
  691. "no space before equals sign");
  692. }
  693. # check for plus signs without spaces next to it
  694. if($nostr =~ /(.*)[^+]\+[a-z0-9]/i) {
  695. checkwarn("PLUSNOSPACE",
  696. $line, length($1)+1, $file, $ol,
  697. "no space after plus sign");
  698. }
  699. # check for plus sign without spaces before it
  700. elsif($nostr =~ /(.*)[a-z0-9]\+[^+]/i) {
  701. checkwarn("NOSPACEPLUS",
  702. $line, length($1)+1, $file, $ol,
  703. "no space before plus sign");
  704. }
  705. # check for semicolons without space next to it
  706. if($nostr =~ /(.*)\;[a-z0-9]/i) {
  707. checkwarn("SEMINOSPACE",
  708. $line, length($1)+1, $file, $ol,
  709. "no space after semicolon");
  710. }
  711. # typedef struct ... {
  712. if($nostr =~ /^(.*)typedef struct.*{/) {
  713. checkwarn("TYPEDEFSTRUCT",
  714. $line, length($1)+1, $file, $ol,
  715. "typedef'ed struct");
  716. }
  717. if($nostr =~ /(.*)! +(\w|\()/) {
  718. checkwarn("EXCLAMATIONSPACE",
  719. $line, length($1)+1, $file, $ol,
  720. "space after exclamation mark");
  721. }
  722. # check for more than one consecutive space before open brace or
  723. # question mark. Skip lines containing strings since they make it hard
  724. # due to artificially getting multiple spaces
  725. if(($l eq $nostr) &&
  726. $nostr =~ /^(.*(\S)) + [{?]/i) {
  727. checkwarn("MULTISPACE",
  728. $line, length($1)+1, $file, $ol,
  729. "multiple spaces");
  730. }
  731. preproc:
  732. $line++;
  733. $prevp = $prep;
  734. $prevl = $ol if(!$prep);
  735. $prevpl = $ol if($prep);
  736. }
  737. if(!scalar(@copyright)) {
  738. checkwarn("COPYRIGHT", 1, 0, $file, "", "Missing copyright statement", 1);
  739. }
  740. # COPYRIGHTYEAR is a extended warning so we must first see if it has been
  741. # enabled in .checksrc
  742. if(defined($warnings{"COPYRIGHTYEAR"})) {
  743. # The check for updated copyrightyear is overly complicated in order to
  744. # not punish current hacking for past sins. The copyright years are
  745. # right now a bit behind, so enforcing copyright year checking on all
  746. # files would cause hundreds of errors. Instead we only look at files
  747. # which are tracked in the Git repo and edited in the workdir, or
  748. # committed locally on the branch without being in upstream master.
  749. #
  750. # The simple and naive test is to simply check for the current year,
  751. # but updating the year even without an edit is against project policy
  752. # (and it would fail every file on January 1st).
  753. #
  754. # A rather more interesting, and correct, check would be to not test
  755. # only locally committed files but inspect all files wrt the year of
  756. # their last commit. Removing the `git rev-list origin/master..HEAD`
  757. # condition below will enforce copyright year checks against the year
  758. # the file was last committed (and thus edited to some degree).
  759. my $commityear = undef;
  760. @copyright = sort {$$b{year} cmp $$a{year}} @copyright;
  761. # if the file is modified, assume commit year this year
  762. if(`git status -s -- $file` =~ /^ [MARCU]/) {
  763. $commityear = (localtime(time))[5] + 1900;
  764. }
  765. else {
  766. # min-parents=1 to ignore wrong initial commit in truncated repos
  767. my $grl = `git rev-list --max-count=1 --min-parents=1 --timestamp HEAD -- $file`;
  768. if($grl) {
  769. chomp $grl;
  770. $commityear = (localtime((split(/ /, $grl))[0]))[5] + 1900;
  771. }
  772. }
  773. if(defined($commityear) && scalar(@copyright) &&
  774. $copyright[0]{year} != $commityear) {
  775. checkwarn("COPYRIGHTYEAR", $copyright[0]{line}, $copyright[0]{col},
  776. $file, $copyright[0]{code},
  777. "Copyright year out of date, should be $commityear, " .
  778. "is $copyright[0]{year}", 1);
  779. }
  780. }
  781. if($incomment) {
  782. checkwarn("OPENCOMMENT", 1, 0, $file, "", "Missing closing comment", 1);
  783. }
  784. checksrc_endoffile($file);
  785. close(R);
  786. }
  787. if($errors || $warnings || $verbose) {
  788. printf "checksrc: %d errors and %d warnings\n", $errors, $warnings;
  789. if($suppressed) {
  790. printf "checksrc: %d errors and %d warnings suppressed\n",
  791. $serrors,
  792. $swarnings;
  793. }
  794. exit 5; # return failure
  795. }