util/ck_errf.pl: add detection of unknown libcrypto and libssl libs
[openssl.git] / util / ck_errf.pl
1 #! /usr/bin/env perl
2 # Copyright 1995-2016 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
9 # This is just a quick script to scan for cases where the 'error'
10 # function name in a XXXerr() macro is wrong.
11 #
12 # Run in the top level by going
13 # perl util/ck_errf.pl */*.c */*/*.c
14 #
15
16 use strict;
17 use warnings;
18
19 my $err_strict = 0;
20 my $bad        = 0;
21
22 # To detect if there is any error generation for a libcrypto/libssl libs
23 # we don't know, we need to find out what libs we do know.  That list is
24 # readily available in crypto/err/openssl.ec, in form of lines starting
25 # with "L ".
26 my $config     = "crypto/err/openssl.ec";
27 my %libs       = ( "SYS" => 1 );
28 open my $cfh, $config or die "Trying to read $config: $!\n";
29 while (<$cfh>) {
30     s|\R$||;                    # Better chomp
31     next unless m|^L ([0-9A-Z_]+)\s|;
32     next if $1 eq "NONE";
33     $libs{$1} = 1;
34 }
35
36 foreach my $file (@ARGV) {
37     if ( $file eq "-strict" ) {
38         $err_strict = 1;
39         next;
40     }
41     open( IN, "<$file" ) || die "Can't open $file, $!";
42     my $func = "";
43     while (<IN>) {
44         if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) {
45             /^([^()]*(\([^()]*\)[^()]*)*)\(/;
46             $1 =~ /([A-Za-z_0-9]*)$/;
47             $func = $1;
48             $func =~ tr/A-Z/a-z/;
49         }
50         if ( /([A-Z0-9_]+[A-Z0-9])err\(([^,]+)/ && !/ckerr_ignore/ ) {
51             my $errlib = $1;
52             my $n      = $2;
53
54             unless ( $libs{$errlib} ) {
55                 print "$file:$.:$errlib unknown\n";
56                 $bad = 1;
57             }
58
59             if ( $func eq "" ) {
60                 print "$file:$.:???:$n\n";
61                 $bad = 1;
62                 next;
63             }
64
65             if ( $n !~ /^(.+)_F_(.+)$/ ) {
66                 #print "check -$file:$.:$func:$n\n";
67                 next;
68             }
69             my $lib = $1;
70             $n   = $2;
71
72             if ( $lib ne $errlib ) {
73                 print "$file:$.:$func:$n [${errlib}err]\n";
74                 $bad = 1;
75                 next;
76             }
77
78             $n =~ tr/A-Z/a-z/;
79             if ( $n ne $func && $errlib ne "SYS" ) {
80                 print "$file:$.:$func:$n\n";
81                 $bad = 1;
82                 next;
83             }
84
85             #           print "$func:$1\n";
86         }
87     }
88     close(IN);
89 }
90
91 if ( $bad && $err_strict ) {
92     print STDERR "FATAL: error discrepancy\n";
93     exit 1;
94 }