2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 10:10:06 +00:00

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()`.
This commit is contained in:
Tony Finch 2023-06-09 13:58:41 +01:00 committed by Ondřej Surý
parent 6272482113
commit a742fde51a
No known key found for this signature in database
GPG Key ID: 2820F37E873DEA41

161
cocci/isc_mem_cget.spatch Normal file
View File

@ -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))