#!/usr/bin/env perl1#***************************************************************************2# _ _ ____ _3# Project ___| | | | _ \| |4# / __| | | | |_) | |5# | (__| |_| | _ <| |___6# \___|\___/|_| \_\_____|7#8# Copyright (C) Daniel Stenberg, <[email protected]>, et al.9#10# This software is licensed as described in the file COPYING, which11# you should have received as part of this distribution. The terms12# are also available at https://curl.se/docs/copyright.html.13#14# You may opt to use, copy, modify, merge, publish, distribute and/or sell15# copies of the Software, and permit persons to whom the Software is16# furnished to do so, under the terms of the COPYING file.17#18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY19# KIND, either express or implied.20#21# SPDX-License-Identifier: curl22#23###########################################################################24#25# This script scans C source files. If they seem to use memory functions,26# it also makes sure that it #includes the correct two header files!27#28# You can also mark a C source as fine by using 'mem-include-scan' anywhere in29# it.30#3132use strict;33use warnings;3435my $dir = $ARGV[0] || die "specify directory!";3637sub scanfile {38my ($file) = @_;39my $memfunc;40my $memdebug;41my $curlmem;4243print STDERR "checking $file...\n";4445open(my $f, "<", "$file");46while(<$f>) {47if($_ =~ /\W(free|alloc|strdup)\(/) {48$memfunc++;49}50elsif($_ =~ /^ *# *include \"memdebug.h\"/) {51$memdebug++;52}53elsif($_ =~ /^ *# *include \"curl_memory.h\"/) {54$curlmem++;55}56elsif($_ =~ /mem-include-scan/) {57# free pass58close($f);59return 0;60}61if($memfunc && $memdebug && $curlmem) {62last;63}64}65close($f);666768if($memfunc) {69if($memdebug && $curlmem) {70return 0;71}72else {73if(!$memdebug) {74print STDERR "$file doesn't include \"memdebug.h\"!\n";75}76if(!$curlmem) {77print STDERR "$file doesn't include \"curl_memory.h\"!\n";78}79return 1;80}81}82return 0;83}8485opendir(my $dh, $dir) || die "can't opendir $dir: $!";86my @cfiles = grep { /\.c\z/ && -f "$dir/$_" } readdir($dh);87closedir $dh;8889my $errs;90for(@cfiles) {91$errs += scanfile("$dir/$_");92}9394if($errs) {95print STDERR "----\n$errs errors detected!\n";96exit 2;97}9899100