2
0

ck_errf.pl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #! /usr/bin/env perl
  2. # Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # This is just a quick script to scan for cases where the 'error'
  9. # function name in a XXXerr() macro is wrong.
  10. #
  11. # Run in the top level by going
  12. # perl util/ck_errf.pl */*.c */*/*.c
  13. #
  14. use strict;
  15. use warnings;
  16. my $config;
  17. my $err_strict = 0;
  18. my $debug = 0;
  19. my $internal = 0;
  20. sub help
  21. {
  22. print STDERR <<"EOF";
  23. mkerr.pl [options] [files...]
  24. Options:
  25. -conf FILE Use the named config file FILE instead of the default.
  26. -debug Verbose output debugging on stderr.
  27. -internal Generate code that is to be built as part of OpenSSL itself.
  28. Also scans internal list of files.
  29. -strict If any error was found, fail with exit code 1, otherwise 0.
  30. -help Show this help text.
  31. ... Additional arguments are added to the file list to scan,
  32. if '-internal' was NOT specified on the command line.
  33. EOF
  34. }
  35. while ( @ARGV ) {
  36. my $arg = $ARGV[0];
  37. last unless $arg =~ /-.*/;
  38. $arg = $1 if $arg =~ /-(-.*)/;
  39. if ( $arg eq "-conf" ) {
  40. $config = $ARGV[1];
  41. shift @ARGV;
  42. } elsif ( $arg eq "-debug" ) {
  43. $debug = 1;
  44. } elsif ( $arg eq "-internal" ) {
  45. $internal = 1;
  46. } elsif ( $arg eq "-strict" ) {
  47. $err_strict = 1;
  48. } elsif ( $arg =~ /-*h(elp)?/ ) {
  49. &help();
  50. exit;
  51. } elsif ( $arg =~ /-.*/ ) {
  52. die "Unknown option $arg; use -h for help.\n";
  53. }
  54. shift @ARGV;
  55. }
  56. my @source;
  57. if ( $internal ) {
  58. die "Extra parameters given.\n" if @ARGV;
  59. $config = "crypto/err/openssl.ec" unless defined $config;
  60. @source = ( glob('crypto/*.c'), glob('crypto/*/*.c'),
  61. glob('ssl/*.c'), glob('ssl/*/*.c') );
  62. } else {
  63. die "Configuration file not given.\nSee '$0 -help' for information\n"
  64. unless defined $config;
  65. @source = @ARGV;
  66. }
  67. # To detect if there is any error generation for a libcrypto/libssl libs
  68. # we don't know, we need to find out what libs we do know. That list is
  69. # readily available in crypto/err/openssl.ec, in form of lines starting
  70. # with "L ". Note that we always rely on the modules SYS and ERR to be
  71. # generally available.
  72. my %libs = ( SYS => 1, ERR => 1 );
  73. open my $cfh, $config or die "Trying to read $config: $!\n";
  74. while (<$cfh>) {
  75. s|\R$||; # Better chomp
  76. next unless m|^L ([0-9A-Z_]+)\s|;
  77. next if $1 eq "NONE";
  78. $libs{$1} = 1;
  79. }
  80. my $bad = 0;
  81. foreach my $file (@source) {
  82. open( IN, "<$file" ) || die "Can't open $file, $!";
  83. my $func = "";
  84. while (<IN>) {
  85. if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) {
  86. /^([^()]*(\([^()]*\)[^()]*)*)\(/;
  87. $1 =~ /([A-Za-z_0-9]*)$/;
  88. $func = $1;
  89. $func =~ tr/A-Z/a-z/;
  90. }
  91. if ( /([A-Z0-9_]+[A-Z0-9])err\(([^,]+)/ && !/ckerr_ignore/ ) {
  92. my $errlib = $1;
  93. my $n = $2;
  94. unless ( $libs{$errlib} ) {
  95. print "$file:$.:$errlib not listed in $config\n";
  96. $libs{$errlib} = 1; # To not display it again
  97. $bad = 1;
  98. }
  99. if ( $func eq "" ) {
  100. print "$file:$.:???:$n\n";
  101. $bad = 1;
  102. next;
  103. }
  104. if ( $n !~ /^(.+)_F_(.+)$/ ) {
  105. #print "check -$file:$.:$func:$n\n";
  106. next;
  107. }
  108. my $lib = $1;
  109. $n = $2;
  110. if ( $lib ne $errlib ) {
  111. print "$file:$.:$func:$n [${errlib}err]\n";
  112. $bad = 1;
  113. next;
  114. }
  115. $n =~ tr/A-Z/a-z/;
  116. if ( $n ne $func && $errlib ne "SYS" ) {
  117. print "$file:$.:$func:$n\n";
  118. $bad = 1;
  119. next;
  120. }
  121. # print "$func:$1\n";
  122. }
  123. }
  124. close(IN);
  125. }
  126. if ( $bad && $err_strict ) {
  127. print STDERR "FATAL: error discrepancy\n";
  128. exit 1;
  129. }