I would like to see a small but complete snippet of code that will cause Clang’s static analyser to complain. My motivation is mostly that I’m trying to get it to work on my PIC32 code, and I need a way to distinguish between “all the code is fine” and “it’s not actually doing anything”. It’s also partly curiosity, since I can’t seem to come up with a simple example myself.
C89/ANSI or C99 is fine, and ideally I’d like to see it pick up a simple memory leak. My usage is
clang --analyze test.c
I found a “bug” in my code (the only one 😉 that triggers by that, and that is not detected by
-Wall. I cooked it down to the followingThis is a relatively straight forward implementation of a linked list, but this is not important here. The variable
myheadis unused in a common sense application of the term, but for the compiler it is used since inside the initializer the address of a field is taken.clangcorrectly analyzes this asEdit: I found another one that also detects stack memory proliferation
This is not detected by
gcc,open64orclangwith-Wall, but byclangwith--analyze.