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