1// Find and fix trivial use-after-free of Error objects
2//
3// Copyright (c) 2020 Virtuozzo International GmbH.
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License as
7// published by the Free Software Foundation; either version 2 of the
8// License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program.  If not, see
17// <http://www.gnu.org/licenses/>.
18//
19// How to use:
20// spatch --sp-file scripts/coccinelle/error-use-after-free.cocci \
21//  --macro-file scripts/cocci-macro-file.h --in-place \
22//  --no-show-diff ( FILES... | --use-gitgrep . )
23
24@ exists@
25identifier fn, fn2;
26expression err;
27@@
28
29 fn(...)
30 {
31     <...
32(
33     error_free(err);
34+    err = NULL;
35|
36     error_report_err(err);
37+    err = NULL;
38|
39     error_reportf_err(err, ...);
40+    err = NULL;
41|
42     warn_report_err(err);
43+    err = NULL;
44|
45     warn_reportf_err(err, ...);
46+    err = NULL;
47)
48     ... when != err = NULL
49         when != exit(...)
50     fn2(..., err, ...)
51     ...>
52 }
53