While revisiting some codes I've written, I noticed that the build commands in the test scripts did not correctly invoke the scan-build
command. The formation of a revision is ready, but I have some question with regard to the capability of scan-build
and the Clang static analyzer.
Can the analyzer discover errors at link time? How to do that?
For example, within a single source file, it's easy to discover memory allocation errors (leak, double-free, free-after-use, etc.), but can it still discover such errors when it's done through interface functions implemented in another translation unit?
I've written 2 files for testing whether it can do that, but apparently it cannot.
/* memlib.c */
#include <stdlib.h>
void *foo_alloc(int len) { return malloc(len * 4); }
void foo_dealloc(void *foo) { return free(foo); }
/* mem-main.c */
void *foo_alloc(int len);
void foo_dealloc(void *foo);
int main()
{
int *p;
p = foo_alloc(2);
p[1] = 32;
p = foo_alloc(1);
p[0] = 54;
foo_dealloc(p);
p[0] = 47;
foo_dealloc(p);
return 0;
}
The compilation command:
scan-build sh -c '$CC "$@"' foo -o mem-main mem-main.c memlib.c
I'm using the scan-build
from PyPI, but I think that's pretty much irrelevant as it's just a program driver.
As a side note, I'm open to tool recommendations where link-time analysis can be performed.