mem-include-scan.pl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 2010 - 2020, 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.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. use strict;
  31. use warnings;
  32. my $dir = $ARGV[0] || die "specify directory!";
  33. sub scanfile {
  34. my ($file) = @_;
  35. my $memfunc;
  36. my $memdebug;
  37. my $curlmem;
  38. print STDERR "checking $file...\n";
  39. open(F, "<$file");
  40. while(<F>) {
  41. if($_ =~ /(free|alloc|strdup)\(/) {
  42. $memfunc++;
  43. }
  44. elsif($_ =~ /^ *# *include \"memdebug.h\"/) {
  45. $memdebug++;
  46. }
  47. elsif($_ =~ /^ *# *include \"curl_memory.h\"/) {
  48. $curlmem++;
  49. }
  50. elsif($_ =~ /mem-include-scan/) {
  51. # free pass
  52. close(F);
  53. return 0;
  54. }
  55. if($memfunc && $memdebug && $curlmem) {
  56. last;
  57. }
  58. }
  59. close(F);
  60. if($memfunc) {
  61. if($memdebug && $curlmem) {
  62. return 0;
  63. }
  64. else {
  65. if(!$memdebug) {
  66. print STDERR "$file doesn't include \"memdebug.h\"!\n";
  67. }
  68. if(!$curlmem) {
  69. print STDERR "$file doesn't include \"curl_memory.h\"!\n";
  70. }
  71. return 1;
  72. }
  73. }
  74. return 0;
  75. }
  76. opendir(my $dh, $dir) || die "can't opendir $dir: $!";
  77. my @cfiles = grep { /\.c\z/ && -f "$dir/$_" } readdir($dh);
  78. closedir $dh;
  79. my $errs;
  80. for(@cfiles) {
  81. $errs += scanfile("$dir/$_");
  82. }
  83. if($errs) {
  84. print STDERR "----\n$errs errors detected!\n";
  85. exit 2;
  86. }