Skip to content

0.5.0

Choose a tag to compare

@sonic182 sonic182 released this 10 Nov 17:33
· 52 commits to main since this release
6848a76

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_OWNS fields 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 cleanup function is followed; pair_free(&p) at a CC_FREES parameter, memset(&p, 0, sizeof p) and p = (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 for b: freeing both is a double free, and find(&key); free(name); is clean.
  • Nested structs: a struct in a struct by value (p.inner.left), up to 8 levels and 16 CC_OWNS fields.
  • 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_OWNS on a struct parameter makes its CC_OWNS fields own from the entry of the function, and the caller hands them on.

Breaking:

  • These rules give new CC-OWN-LEAK, CC-OWN-DOUBLE-FREE and CC-OWN-USE-AFTER-FREE findings on code that was clean before.
  • Run --update-baseline once 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.json

Changelog

Changed

  • Breaking: the ownership rules follow the CC_OWNS fields 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-baseline once
    after upgrading.
  • Breaking: a local struct with a cleanup function, or whose CC_OWNS field has its address taken
    (free_and_null(&p.left), take(&p.left)), is followed too; the cleanup function, pair_free(&p) at a CC_FREES
    parameter, memset(&p, 0, sizeof p) and p = (struct pair){...} free or clear its fields. A field freed twice, or
    one that still owns at a clear, is a finding. Run --update-baseline once 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-baseline once after upgrading.
  • Breaking: a CC_OWNS field 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-baseline once after upgrading.
  • Breaking: a struct in a local struct by value (p.inner.left, up to 8 levels and 16 CC_OWNS fields 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) and pair_free(&p.inner) work on
    the fields of that member, and a nested initializer ({ .inner = { .left = b } }) gives each its item. Run
    --update-baseline once after upgrading.
  • Breaking: a local array of structs of a constant size (struct pair a[2], up to 16 CC_OWNS fields 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, and memset(a, 0, sizeof a) clears it. Run --update-baseline once after upgrading.
  • Breaking: CC_OWNS on a struct parameter passed by value (void f(CC_OWNS struct pair p)) says that the
    function owns its CC_OWNS fields from the entry (p.left), not the struct as one pointer; a caller that passes a
    struct there hands its fields on. Run --update-baseline once after upgrading.
  • The items of p = (struct pair){...} that read a field of p (p.left ? p.left : make(n)) are read before the
    store, and a case or default label inside a block of a switch keeps the pointers of that block, as a goto
    into it does.