Repository navigation
0.5.0
cchecker 0.5.0 follows ownership through structs much further: nested structs, arrays of structs and struct parameters passed by value.
- Struct fields up to the escape: the
CC_OWNSfields of a local struct are followed until the struct escapes (its address passed, a copy, a return), and handed on there. A leak before the escape is found. - Cleanup and clears: a struct with a
cleanupfunction is followed;pair_free(&p)at aCC_FREESparameter,memset(&p, 0, sizeof p)andp = (struct pair){...}free or clear its fields. - Structs by declaration: a struct declared in a function body, an unnamed one, and one that shadows another of the same name are followed.
- Fields set from a local: after
p.left = b, the field stands forb: freeing both is a double free, andfind(&key); free(name);is clean. - Nested structs: a struct in a struct by value (
p.inner.left), up to 8 levels and 16CC_OWNSfields. - Arrays of structs: a local array of a constant size (
struct pair a[2]) is followed item by item when the function names its items by constant indexes. - Struct parameters by value:
CC_OWNSon a struct parameter makes itsCC_OWNSfields own from the entry of the function, and the caller hands them on.
Breaking:
- These rules give new
CC-OWN-LEAK,CC-OWN-DOUBLE-FREEandCC-OWN-USE-AFTER-FREEfindings on code that was clean before. - Run
--update-baselineonce after upgrading.
Install and run (needs Agentica 0.27.0 or 0.28.0, gcc 10+, clang and clang-tidy):
git clone --branch 0.5.0 https://github.com/agentica-lang/cchecker ~/cchecker
agentica ~/cchecker/cchecker.agc -p build # in your project, with build/compile_commands.jsonChangelog
Changed
- Breaking: the ownership rules follow the
CC_OWNSfields of a local struct up to where the struct escapes (its
address passed, a copy, a by-value argument, a return,p = q), and hand them on there: a leak before the escape,
or a field that still owns when the whole struct gets a new value, is a CC-OWN-LEAK. Run--update-baselineonce
after upgrading. - Breaking: a local struct with a
cleanupfunction, or whoseCC_OWNSfield has its address taken
(free_and_null(&p.left),take(&p.left)), is followed too; the cleanup function,pair_free(&p)at aCC_FREES
parameter,memset(&p, 0, sizeof p)andp = (struct pair){...}free or clear its fields. A field freed twice, or
one that still owns at a clear, is a finding. Run--update-baselineonce after upgrading. - Breaking: the ownership rules find the struct of a local variable, an initializer and a compound literal by its
declaration, as C finds a tag or a typedef, not by its name: a struct declared in a function body, an unnamed one and
one that shadows another of the same name are followed, and an initializer of a union or of an unnamed member hands
its pointer on. Run--update-baselineonce after upgrading. - Breaking: a
CC_OWNSfield of a local struct set from a local (p.left = b,{ .left = b }) stands for the
local: freeing both is a CC-OWN-DOUBLE-FREE, and when the struct escapes the local is lent with it
(find(&key); free(name);is clean). A field or a local that gets a pointer already freed or handed on
(free(b); char *c = b;) is a CC-OWN-USE-AFTER-FREE, and a call that clears only an alias (zap(&c),
zap(&p.left)) frees the pointer it stands for. Run--update-baselineonce after upgrading. - Breaking: a struct in a local struct by value (
p.inner.left, up to 8 levels and 16CC_OWNSfields in all;
over that only its own fields) and a field of an unnamed struct member (c.buf) are followed as the fields of the
struct are:keep(&p.inner),p.inner = q,memset(&p.inner, 0, sizeof p.inner)andpair_free(&p.inner)work on
the fields of that member, and a nested initializer ({ .inner = { .left = b } }) gives each its item. Run
--update-baselineonce after upgrading. - Breaking: a local array of structs of a constant size (
struct pair a[2], up to 16CC_OWNSfields in all) is
followed item by item when a function names its items by constant indexes only (a[0].left); an index that is
not a constant, the array read whole (keep(a),a + 1) or&a[0]anywhere in the function leaves it not
followed there, andmemset(a, 0, sizeof a)clears it. Run--update-baselineonce after upgrading. - Breaking:
CC_OWNSon a struct parameter passed by value (void f(CC_OWNS struct pair p)) says that the
function owns itsCC_OWNSfields from the entry (p.left), not the struct as one pointer; a caller that passes a
struct there hands its fields on. Run--update-baselineonce after upgrading. - The items of
p = (struct pair){...}that read a field ofp(p.left ? p.left : make(n)) are read before the
store, and acaseordefaultlabel inside a block of aswitchkeeps the pointers of that block, as agoto
into it does.