aboutsummaryrefslogtreecommitdiff
path: root/scripts/coccinelle/calloc.cocci
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/coccinelle/calloc.cocci')
-rw-r--r--scripts/coccinelle/calloc.cocci23
1 files changed, 23 insertions, 0 deletions
diff --git a/scripts/coccinelle/calloc.cocci b/scripts/coccinelle/calloc.cocci
new file mode 100644
index 0000000000..fbda88e538
--- /dev/null
+++ b/scripts/coccinelle/calloc.cocci
@@ -0,0 +1,23 @@
+// Use calloc or realloc as appropriate instead of multiply-and-alloc
+
+@malloc_to_calloc@
+identifier f =~ "(tor_malloc|tor_malloc_zero)";
+expression a;
+constant b;
+@@
+- f(a * b)
++ tor_calloc(a, b)
+
+@calloc_arg_order@
+expression a;
+type t;
+@@
+- tor_calloc(sizeof(t), a)
++ tor_calloc(a, sizeof(t))
+
+@realloc_to_reallocarray@
+expression a, b;
+expression p;
+@@
+- tor_realloc(p, a * b)
++ tor_reallocarray(p, a, b)