2
0

mem-include-scan.pl 2.6 KB

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