blob: 2c39c14340fd68be955164a58f22d1f25302d917 [file] [log] [blame]
Manuel Pégourié-Gonnardfd60a5c2014-11-12 22:54:24 +01001#!/usr/bin/perl
2
3# Find functions making recursive calls to themselves.
4# (Multiple recursion where a() calls b() which calls a() not covered.)
5#
6# When the recursion depth might depend on data controlled by the attacker in
7# an unbounded way, those functions should use interation instead.
8#
9# Typical usage: scripts/recursion.pl library/*.c
10
11use warnings;
12use strict;
13
14use utf8;
15use open qw(:std utf8);
16
17# exclude functions that are ok:
18# - mpi_write_hlp: bounded by size of mpi, a compile-time constant
Manuel Pégourié-Gonnard10c44d72014-11-20 17:30:37 +010019# - x509_crt_verify_child: bounded by POLARSSL_X509_MAX_INTERMEDIATE_CA
20my $known_ok = qr/mpi_write_hlp|x509_crt_verify_child/;
Manuel Pégourié-Gonnardfd60a5c2014-11-12 22:54:24 +010021
22my $cur_name;
23my $inside;
24my @funcs;
25
26die "Usage: $0 file.c [...]\n" unless @ARGV;
27
28while (<>)
29{
30 if( /^[^\/#{}\s]/ && ! /\[.*]/ ) {
31 chomp( $cur_name = $_ ) unless $inside;
32 } elsif( /^{/ && $cur_name ) {
33 $inside = 1;
34 $cur_name =~ s/.* ([^ ]*)\(.*/$1/;
35 } elsif( /^}/ && $inside ) {
36 undef $inside;
37 undef $cur_name;
38 } elsif( $inside && /\b\Q$cur_name\E\([^)]/ ) {
39 push @funcs, $cur_name unless /$known_ok/;
40 }
41}
42
43print "$_\n" for @funcs;
44exit @funcs;