This duplication was motivated by the following:
- FIXME from unix.API stating "Eventually these should be rolled into the MallocChecker, but right now they're more basic and valuable for widespread use."
- an idea to extend the check to handle zero size allocation by 'new' and 'new' in the sequel.
- create the base for implementation of undefbehavior.ZeroAllocDereference checker.
The patch is a scratch, may it make sense to make '0 size allocation' the separate checker to at least preserve handling of zero-size realloc unchanged.
I suggest the following logic for handling zero-size realloc:
- if the returned value of realloc(p, 0) is not assigned then silently treat it as simple 'free' (the old behavior of unix.Malloc)
- if the returned value is assigned then fire '0 size allocation' warning as the usage of the returned pointer is not safe.
Specification C11 n1570, 126.96.36.199 says: "If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object."
Usage of the returned pointer is planned to be a matter of other checkers (undefbehavior.ZeroAllocDereference + maybe another checker that checks for usage of returned pointer in comparisons)