2
0

ck_errf.pl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #! /usr/bin/env perl
  2. # Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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'), glob('providers/*.c'),
  62. glob('providers/*/*.c'), glob('providers/*/*/*.c') );
  63. } else {
  64. die "Configuration file not given.\nSee '$0 -help' for information\n"
  65. unless defined $config;
  66. @source = @ARGV;
  67. }
  68. # To detect if there is any error generation for a libcrypto/libssl libs
  69. # we don't know, we need to find out what libs we do know. That list is
  70. # readily available in crypto/err/openssl.ec, in form of lines starting
  71. # with "L ". Note that we always rely on the modules SYS and ERR to be
  72. # generally available.
  73. my %libs = ( SYS => 1, ERR => 1 );
  74. open my $cfh, $config or die "Trying to read $config: $!\n";
  75. while (<$cfh>) {
  76. s|\R$||; # Better chomp
  77. next unless m|^L ([0-9A-Z_]+)\s|;
  78. next if $1 eq "NONE";
  79. $libs{$1} = 1;
  80. }
  81. my $bad = 0;
  82. foreach my $file (@source) {
  83. open( IN, "<$file" ) || die "Can't open $file, $!";
  84. my $func = "";
  85. while (<IN>) {
  86. if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) {
  87. /^([^()]*(\([^()]*\)[^()]*)*)\(/;
  88. $1 =~ /([A-Za-z_0-9]*)$/;
  89. $func = $1;
  90. $func =~ tr/A-Z/a-z/;
  91. }
  92. if ( /([A-Z0-9_]+[A-Z0-9])err\(([^,]+)/ && !/ckerr_ignore/ ) {
  93. my $errlib = $1;
  94. my $n = $2;
  95. unless ( $libs{$errlib} ) {
  96. print "$file:$.:$errlib not listed in $config\n";
  97. $libs{$errlib} = 1; # To not display it again
  98. $bad = 1;
  99. }
  100. if ( $func eq "" ) {
  101. print "$file:$.:???:$n\n";
  102. $bad = 1;
  103. next;
  104. }
  105. if ( $n !~ /^(.+)_F_(.+)$/ ) {
  106. #print "check -$file:$.:$func:$n\n";
  107. next;
  108. }
  109. my $lib = $1;
  110. $n = $2;
  111. if ( $lib ne $errlib ) {
  112. print "$file:$.:$func:$n [${errlib}err]\n";
  113. $bad = 1;
  114. next;
  115. }
  116. $n =~ tr/A-Z/a-z/;
  117. if ( $n ne $func && $errlib ne "SYS" ) {
  118. print "$file:$.:$func:$n\n";
  119. $bad = 1;
  120. next;
  121. }
  122. # print "$func:$1\n";
  123. }
  124. }
  125. close(IN);
  126. }
  127. if ( $bad && $err_strict ) {
  128. print STDERR "FATAL: error discrepancy\n";
  129. exit 1;
  130. }