1#!/usr/bin/env perl
2#***************************************************************************
3#                                  _   _ ____  _
4#  Project                     ___| | | |  _ \| |
5#                             / __| | | | |_) | |
6#                            | (__| |_| |  _ <| |___
7#                             \___|\___/|_| \_\_____|
8#
9# Copyright (C) 2010-2012, 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 http://curl.haxx.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###########################################################################
23#
24# This script scans C source files. If they seem to use memory functions,
25# it also makes sure that it #includes the correct two header files!
26#
27# You can also mark a C source as fine by using 'mem-include-scan' anywhere in
28# it.
29#
30
31use strict;
32use warnings;
33
34my $dir = $ARGV[0] || die "specify directory!";
35
36sub scanfile {
37    my ($file) = @_;
38    my $memfunc;
39    my $memdebug;
40    my $curlmem;
41
42    print STDERR "checking $file...\n";
43
44    open(F, "<$file");
45    while(<F>) {
46        if($_ =~ /(free|alloc|strdup)\(/) {
47            $memfunc++;
48        }
49        elsif($_ =~ /^ *# *include \"memdebug.h\"/) {
50            $memdebug++;
51        }
52        elsif($_ =~ /^ *# *include \"curl_memory.h\"/) {
53            $curlmem++;
54        }
55        elsif($_ =~ /mem-include-scan/) {
56            # free pass
57            close(F);
58            return 0;
59        }
60        if($memfunc && $memdebug && $curlmem) {
61            last;
62        }
63    }
64    close(F);
65
66
67    if($memfunc) {
68        if($memdebug && $curlmem) {
69            return 0;
70        }
71        else {
72            if(!$memdebug) {
73                print STDERR "$file doesn't include \"memdebug.h\"!\n";
74            }
75            if(!$curlmem) {
76                print STDERR "$file doesn't include \"curl_memory.h\"!\n";
77            }
78            return 1;
79        }
80    }
81    return 0;
82}
83
84opendir(my $dh, $dir) || die "can't opendir $dir: $!";
85my @cfiles = grep { /\.c\z/ && -f "$dir/$_" } readdir($dh);
86closedir $dh;
87
88my $errs;
89for(@cfiles) {
90    $errs += scanfile("$dir/$_");
91}
92
93if($errs) {
94    print STDERR "----\n$errs errors detected!\n";
95    exit 2;
96}
97