]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
A semantic patch to refactor isc_mem_cget and friends
authorTony Finch <fanf@isc.org>
Fri, 9 Jun 2023 12:58:41 +0000 (13:58 +0100)
committerOndřej Surý <ondrej@isc.org>
Thu, 31 Aug 2023 20:08:35 +0000 (22:08 +0200)
The aim is to match unsafe patterns of allocation size arithmetic
and turn them into safe calls to the new `isc_mem_cget()`,
`isc_mem_creget()`, and `isc_mem_cput()`.

cocci/isc_mem_cget.spatch [new file with mode: 0644]

diff --git a/cocci/isc_mem_cget.spatch b/cocci/isc_mem_cget.spatch
new file mode 100644 (file)
index 0000000..af2fb5b
--- /dev/null
@@ -0,0 +1,161 @@
+@@
+expression MCTX, COUNT;
+type ELEM;
+@@
+
+- isc_mem_get(MCTX, COUNT * sizeof(ELEM))
++ isc_mem_cget(MCTX, COUNT, sizeof(ELEM))
+
+@@
+expression MCTX, COUNT, ELEM;
+@@
+
+- isc_mem_get(MCTX, COUNT * sizeof(ELEM))
++ isc_mem_cget(MCTX, COUNT, sizeof(ELEM))
+
+@@
+expression MCTX, OLD_PTR, COUNT;
+type ELEM;
+@@
+
+- isc_mem_put(MCTX, OLD_PTR, COUNT * sizeof(ELEM))
++ isc_mem_cput(MCTX, OLD_PTR, COUNT, sizeof(ELEM))
+
+@@
+expression MCTX, OLD_PTR, COUNT, ELEM;
+@@
+
+- isc_mem_put(MCTX, OLD_PTR, COUNT * sizeof(ELEM))
++ isc_mem_cput(MCTX, OLD_PTR, COUNT, sizeof(ELEM))
+
+@@
+expression MCTX, COUNT;
+type ELEM;
+@@
+
+- isc_mem_get(MCTX, sizeof(ELEM) * COUNT)
++ isc_mem_cget(MCTX, COUNT, sizeof(ELEM))
+
+@@
+expression MCTX, COUNT, ELEM;
+@@
+
+- isc_mem_get(MCTX, sizeof(ELEM) * COUNT)
++ isc_mem_cget(MCTX, COUNT, sizeof(ELEM))
+
+@@
+expression MCTX, OLD_PTR, COUNT;
+type ELEM;
+@@
+
+- isc_mem_put(MCTX, OLD_PTR, sizeof(ELEM) * COUNT)
++ isc_mem_cput(MCTX, OLD_PTR, COUNT, sizeof(ELEM))
+
+@@
+expression MCTX, OLD_PTR, COUNT, ELEM;
+@@
+
+- isc_mem_put(MCTX, OLD_PTR, sizeof(ELEM) * COUNT)
++ isc_mem_cput(MCTX, OLD_PTR, COUNT, sizeof(ELEM))
+
+@@
+expression MCTX, OLD_PTR, NEW_PTR, OLD_COUNT, NEW_COUNT;
+identifier OLD_SIZE, NEW_SIZE;
+type ELEM;
+@@
+
+- size_t OLD_SIZE = OLD_COUNT * sizeof(ELEM);
+- size_t NEW_SIZE = NEW_COUNT * sizeof(ELEM);
+- NEW_PTR = isc_mem_reget(MCTX, OLD_PTR, OLD_SIZE, NEW_SIZE);
++ NEW_PTR = isc_mem_creget(MCTX, OLD_PTR, OLD_COUNT, NEW_COUNT, sizeof(ELEM));
+
+@@
+expression MCTX, OLD_PTR, NEW_PTR, OLD_COUNT, NEW_COUNT, ELEM;
+identifier OLD_SIZE, NEW_SIZE;
+@@
+
+- size_t OLD_SIZE = OLD_COUNT * sizeof(ELEM);
+- size_t NEW_SIZE = NEW_COUNT * sizeof(ELEM);
+- NEW_PTR = isc_mem_reget(MCTX, OLD_PTR, OLD_SIZE, NEW_SIZE);
++ NEW_PTR = isc_mem_creget(MCTX, OLD_PTR, OLD_COUNT, NEW_COUNT, sizeof(ELEM));
+
+@@
+expression MCTX, OLD_PTR, NEW_PTR, OLD_COUNT, NEW_COUNT;
+identifier OLD_SIZE, NEW_SIZE;
+type ELEM;
+@@
+
+- size_t NEW_SIZE = NEW_COUNT * sizeof(ELEM);
+- size_t OLD_SIZE = OLD_COUNT * sizeof(ELEM);
+- NEW_PTR = isc_mem_reget(MCTX, OLD_PTR, OLD_SIZE, NEW_SIZE);
++ NEW_PTR = isc_mem_creget(MCTX, OLD_PTR, OLD_COUNT, NEW_COUNT, sizeof(ELEM));
+
+@@
+expression MCTX, OLD_PTR, NEW_PTR, OLD_COUNT, NEW_COUNT, ELEM;
+identifier OLD_SIZE, NEW_SIZE;
+@@
+
+- size_t NEW_SIZE = NEW_COUNT * sizeof(ELEM);
+- size_t OLD_SIZE = OLD_COUNT * sizeof(ELEM);
+- NEW_PTR = isc_mem_reget(MCTX, OLD_PTR, OLD_SIZE, NEW_SIZE);
++ NEW_PTR = isc_mem_creget(MCTX, OLD_PTR, OLD_COUNT, NEW_COUNT, sizeof(ELEM));
+
+@@
+expression MCTX, OLD_PTR, NEW_PTR, OLD_SIZE, OLD_COUNT, NEW_SIZE, NEW_COUNT;
+type ELEM;
+@@
+
+- OLD_SIZE = OLD_COUNT * sizeof(ELEM);
+- NEW_SIZE = NEW_COUNT * sizeof(ELEM);
+- NEW_PTR = isc_mem_reget(MCTX, OLD_PTR, OLD_SIZE, NEW_SIZE);
++ NEW_PTR = isc_mem_creget(MCTX, OLD_PTR, OLD_COUNT, NEW_COUNT, sizeof(ELEM));
+
+@@
+expression MCTX, OLD_PTR, NEW_PTR, OLD_SIZE, OLD_COUNT, NEW_SIZE, NEW_COUNT, ELEM;
+@@
+
+- OLD_SIZE = OLD_COUNT * sizeof(ELEM);
+- NEW_SIZE = NEW_COUNT * sizeof(ELEM);
+- NEW_PTR = isc_mem_reget(MCTX, OLD_PTR, OLD_SIZE, NEW_SIZE);
++ NEW_PTR = isc_mem_creget(MCTX, OLD_PTR, OLD_COUNT, NEW_COUNT, sizeof(ELEM));
+
+@@
+expression MCTX, OLD_PTR, NEW_PTR, OLD_SIZE, OLD_COUNT, NEW_SIZE, NEW_COUNT;
+type ELEM;
+@@
+
+- NEW_SIZE = NEW_COUNT * sizeof(ELEM);
+- OLD_SIZE = OLD_COUNT * sizeof(ELEM);
+- NEW_PTR = isc_mem_reget(MCTX, OLD_PTR, OLD_SIZE, NEW_SIZE);
++ NEW_PTR = isc_mem_reget(MCTX, OLD_PTR, OLD_COUNT, NEW_COUNT, sizeof(ELEM));
+
+@@
+expression MCTX, OLD_PTR, NEW_PTR, OLD_SIZE, OLD_COUNT, NEW_SIZE, NEW_COUNT, ELEM;
+@@
+
+- NEW_SIZE = NEW_COUNT * sizeof(ELEM);
+- OLD_SIZE = OLD_COUNT * sizeof(ELEM);
+- NEW_PTR = isc_mem_reget(MCTX, OLD_PTR, OLD_SIZE, NEW_SIZE);
++ NEW_PTR = isc_mem_creget(MCTX, OLD_PTR, OLD_COUNT, NEW_COUNT, sizeof(ELEM));
+
+@@
+expression MCTX, OLD_PTR, OLD_COUNT, NEW_COUNT;
+type ELEM;
+@@
+
+- isc_mem_reget(MCTX, OLD_PTR, OLD_COUNT * sizeof(ELEM), NEW_COUNT * sizeof(ELEM))
++ isc_mem_creget(MCTX, OLD_PTR, OLD_COUNT, NEW_COUNT, sizeof(ELEM))
+
+@@
+expression MCTX, OLD_PTR, OLD_COUNT, NEW_COUNT, ELEM;
+@@
+
+- isc_mem_reget(MCTX, OLD_PTR, OLD_COUNT * sizeof(ELEM), NEW_COUNT * sizeof(ELEM))
++ isc_mem_creget(MCTX, OLD_PTR, OLD_COUNT, NEW_COUNT, sizeof(ELEM))
+
+@@
+expression MCTX, OLD_PTR, OLD_SIZE, NEW_SIZE;
+@@
+
+- isc_mem_reget(MCTX, OLD_PTR, OLD_SIZE, NEW_SIZE)
++ isc_mem_creget(MCTX, OLD_PTR, OLD_SIZE, NEW_SIZE, sizeof(char))