From: Tony Finch Date: Fri, 9 Jun 2023 12:58:41 +0000 (+0100) Subject: A semantic patch to refactor isc_mem_cget and friends X-Git-Tag: v9.19.17~18^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a742fde51a979ea187cac40b3278c92b59acb203;p=thirdparty%2Fbind9.git A semantic patch to refactor isc_mem_cget and friends 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()`. --- diff --git a/cocci/isc_mem_cget.spatch b/cocci/isc_mem_cget.spatch new file mode 100644 index 00000000000..af2fb5b224a --- /dev/null +++ b/cocci/isc_mem_cget.spatch @@ -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))