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