4 # Detect instances of "if (p) free (p);".
5 # Likewise "if (p != 0)", "if (0 != p)", or with NULL; and with braces.
7 # Copyright (C) 2008-2021 Free Software Foundation, Inc.
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
22 # Written by Jim Meyering
24 # This is a prologue that allows to run a perl script as an executable
25 # on systems that are compliant to a POSIX version before POSIX:2017.
26 # On such systems, the usual invocation of an executable through execlp()
27 # or execvp() fails with ENOEXEC if it is a script that does not start
28 # with a #! line. The script interpreter mentioned in the #! line has
29 # to be /bin/sh, because on GuixSD systems that is the only program that
30 # has a fixed file name. The second line is essential for perl and is
31 # also useful for editing this file in Emacs. The next two lines below
32 # are valid code in both sh and perl. When executed by sh, they re-execute
33 # the script through the perl program found in $PATH. The '-x' option
34 # is essential as well; without it, perl would re-execute the script
35 # through /bin/sh. When executed by perl, the next two lines are a no-op.
36 eval 'exec perl -wSx "$0" "$@"'
39 my $VERSION = '2021-04-11 10:11'; # UTC
40 # The definition above must lie within the first 8 lines in order
41 # for the Emacs time-stamp write hook (at end) to update it.
42 # If you change this file with Emacs, please let the write hook
43 # do its job. Otherwise, update this string manually.
45 my $copyright_year = '2021';
51 (my $ME = $0) =~ s|.*/||;
53 # use File::Coda; # https://meyering.net/code/Coda/
55 defined fileno STDOUT or return;
56 close STDOUT and return;
57 warn "$ME: failed to close standard output: $!\n";
64 my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
67 print $STREAM "Try '$ME --help' for more information.\n";
72 Usage: $ME [OPTIONS] FILE...
74 Detect any instance in FILE of a useless "if" test before a free call, e.g.,
75 "if (p) free (p);". Any such test may be safely removed without affecting
76 the semantics of the C code in FILE. Use --name=FOO --name=BAR to also
77 detect free-like functions named FOO and BAR.
81 --list print only the name of each matching FILE (\\0-terminated)
82 --name=N add name N to the list of \'free\'-like functions to detect;
85 --help display this help and exit
86 --version output version information and exit
96 For example, this command prints all removable "if" tests before "free"
97 and "kfree" calls in the linux kernel sources:
99 git ls-files -z |xargs -0 $ME --name=kfree
109 return ($expr eq 'NULL' || $expr eq '0');
114 sub EXIT_NO_MATCH {1}
116 my $err = EXIT_NO_MATCH;
122 help => sub { usage 0 },
126 print "$ME version $VERSION\n";
127 print "Copyright (C) $copyright_year Free Software Foundation, Inc.\n";
128 print "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.\n"
129 . "This is free software: you are free to change and redistribute it.\n"
130 . "There is NO WARRANTY, to the extent permitted by law.\n";
132 my $author = "Jim Meyering";
133 print "Written by $author.\n";
140 # Make sure we have the right number of non-option arguments.
141 # Always tell the user why we fail.
143 and (warn "$ME: missing FILE argument\n"), usage EXIT_ERROR;
145 my $or = join '|', @name;
146 my $regexp = qr/(?:$or)/;
148 # Set the input record separator.
149 # Note: this makes it impractical to print line numbers.
154 foreach my $file (@ARGV)
157 or (warn "$ME: can't open '$file' for reading: $!\n"),
158 $err = EXIT_ERROR, next;
159 while (defined (my $line = <FH>))
161 # Skip non-matching lines early to save time
165 /\b(if\s*\(\s*([^)]+?)(?:\s*!=\s*([^)]+?))?\s*\)
167 (?: \s*$regexp\s*\((?:\s*\([^)]+\))?\s*([^)]+)\)\s*;|
168 \s*\{\s*$regexp\s*\((?:\s*\([^)]+\))?\s*([^)]+)\)\s*;\s*\}))/sxg)
171 my ($lhs, $rhs) = ($2, $3);
172 my ($free_opnd, $braced_free_opnd) = ($4, $5);
174 if (!defined $rhs) { $non_NULL = $lhs }
175 elsif (is_NULL $rhs) { $non_NULL = $lhs }
176 elsif (is_NULL $lhs) { $non_NULL = $rhs }
179 # Compare the non-NULL part of the "if" expression and the
180 # free'd expression, without regard to white space.
181 $non_NULL =~ tr/ \t//d;
182 my $e2 = defined $free_opnd ? $free_opnd : $braced_free_opnd;
184 if ($non_NULL eq $e2)
188 and (print "$file\0"), next FILE;
189 print "$file: $all\n";
199 $found_match && $err == EXIT_NO_MATCH
200 and $err = EXIT_MATCH;
206 # The above is to *find* them.
207 # This adjusts them, removing the unnecessary "if (p)" part.
209 # FIXME: do something like this as an option (doesn't do braces):
211 git grep -l -z "$free *(" \
212 | xargs -0 useless-if-before-free -l --name="$free" \
213 | xargs -0 perl -0x3b -pi -e \
214 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*(?:0|NULL))?\s*\)\s+('"$free"'\s*\((?:\s*\([^)]+\))?\s*\1\s*\)\s*;)/$2/s'
216 # Use the following to remove redundant uses of kfree inside braces.
217 # Note that -0777 puts perl in slurp-whole-file mode;
218 # but we have plenty of memory, these days...
220 git grep -l -z "$free *(" \
221 | xargs -0 useless-if-before-free -l --name="$free" \
222 | xargs -0 perl -0777 -pi -e \
223 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*(?:0|NULL))?\s*\)\s*\{\s*('"$free"'\s*\((?:\s*\([^)]+\))?\s*\1\s*\);)\s*\}[^\n]*$/$2/gms'
225 Be careful that the result of the above transformation is valid.
226 If the matched string is followed by "else", then obviously, it won't be.
228 When modifying files, refuse to process anything other than a regular file.
233 ## indent-tabs-mode: nil
234 ## eval: (add-hook 'before-save-hook 'time-stamp)
235 ## time-stamp-line-limit: 50
236 ## time-stamp-start: "my $VERSION = '"
237 ## time-stamp-format: "%:y-%02m-%02d %02H:%02M"
238 ## time-stamp-time-zone: "UTC0"
239 ## time-stamp-end: "'; # UTC"