]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add semantic patch to NULL the destroyed pointer early
authorOndřej Surý <ondrej@isc.org>
Sat, 8 Feb 2020 12:31:51 +0000 (04:31 -0800)
committerOndřej Surý <ondrej@isc.org>
Mon, 10 Feb 2020 02:00:16 +0000 (18:00 -0800)
Our destroy functions usually look like this:

    void
    foo_destroy(foo_t **foop) {
        foo_t foo = *foop;
        ...destroy the contents of foo...
        *foop = NULL;
    }

nulling the pointer should be done as soon as possible which is
not always the case.  This commit adds simple semantic patch that
changes the example function to:

    void
    foo_destroy(foo_t **foop) {
        foo_t foo = *foop;
        *foop = NULL;
        ...destroy the contents of foo...
    }

cocci/null-the-pointer-early.spatch [new file with mode: 0644]

diff --git a/cocci/null-the-pointer-early.spatch b/cocci/null-the-pointer-early.spatch
new file mode 100644 (file)
index 0000000..46fdffc
--- /dev/null
@@ -0,0 +1,21 @@
+@@
+type T;
+T **PP;
+T *P;
+@@
+
+  P = *PP;
++ *PP = NULL;
+  ...
+- *PP = NULL;
+
+@@
+type T;
+identifier PP;
+identifier P;
+@@
+
+  T *P = *PP;
++ *PP = NULL;
+  ...
+- *PP = NULL;