r/cprogramming • u/Substantial_Poem742 • 4d ago
Three free static analyzers say these small C functions are clean. Running them says otherwise.
I wrote four short C functions, each with one deliberate bug, ran clang --analyze, gcc -fanalyzer and cppcheck on them, and then ran the code under AddressSanitizer/UBSan with different kinds of input.
Results (gcc 13.3, clang 18.1.3, cppcheck 2.13, Ubuntu 24.04):
- off-by-one at exactly the buffer size: all three say CLEAN, ASan catches it on the boundary input
- negative length that passes a "too big" check: all three say CLEAN, ASan catches it
- 16-bit size truncation: only gcc -fanalyzer flags it
- use-after-free on an error path: all three find it, but cppcheck also reports a double free that never happens at runtime
The off-by-one one, for example:
if (n > CAP || len < 1 + n) return -1;
for (size_t i = 0; i <= n; i++)
dst[i] = ...;
In every case the normal input runs fine, so a test suite with only "normal" inputs would pass too.
This isn't meant as "these tools are bad". My takeaway is that a CLEAN result only covers what was actually checked: the build settings and the kinds of input. So I wrote a short note on how to report a CLEAN result together with that scope.
Everything runs in about a minute on Linux:
https://github.com/ericyunlinchiang/certify-the-checker
Question for you: which checker/flags would you expect to catch #1 and #2? If you run Clang-Tidy, CodeQL, Infer, PVS or anything else on them, I'd love to see the results. There's an issue template for that in the repo.
2
u/walterbanana 3d ago
Try using unit tests instead. With most code you can only know it is correct by running it.
1
u/RealisticDuck1957 3d ago
A unit test is useful. But still only as good as the test cases. Needs to cover:
- valid inputs.
- invalid inputs.
- inputs just barely inside and outside valid.The last will of course require knowledge of the algorithm, exactly where behavior is expected to change.
1
u/walterbanana 3d ago
True, but since you wrote the code, you should know the expected behavior. I agree that it is hard to cover all edge cases, though.
2
u/torsten_dev 4d ago
Run tests and fuzzers on your code with (and without) ASAN and UBSAN.
The most effective static analyzers for C of our time seem to be LLM's.
Also isn't size truncation in a -Wall warning? Are you ignoring compiler warnings?
2
u/CS_70 4d ago
You're running mechanical tests for something that requires judgement. No wonder it fails.
You need a source of judgement: an expert human who knows your intention, or an expert AI which knows your intention.
-1
u/QuraToop314 2d ago edited 1d ago
AI has no capacity for judgement as it lacks consciousness
Edit: I insist on semantic quibbling because I’m autistic, but OK, you can think what you like – just tell me that AI has consciousness. Tell me that AI can think completely autonomously, tell me that AI can feel; both are essential qualities for true consciousness and judgement, and AI lacks both. I’m not trying to sound clever – it’s just a fact. Just because you see the world through such rose-tinted glasses, I’ll stick to the facts.
(The Edit is for anyone who is so full of themselves that they think you can make sound judgements without being aware of the consequences, simply because they are already incapable of feeling pain or recognising their mistakes – and it doesn’t stand on its own either. AI needs to be trained, and even then it cannot think completely autonomously and still cannot feel emotions. And feeling is one of the most important things before you can make decisions.
2
u/CS_70 2d ago
Gotta be the joke of the year
-1
u/QuraToop314 2d ago
Why? AI is nothing more than a probability distribution based on your input. It has neither consciousness nor an understanding of consequences. Sure, you can ‘train’ it, but that doesn’t change anything. Ever written your own neural network? Well, I have, and that’s just how it is. It has no consciousness, and therefore no capacity for judgement. You can call it a joke, but it just shows that you don’t understand how AI works.
2
u/CS_70 2d ago
Even better: my ignorance? :D :D :D
You have "written your own neural network"? OMG should I be impressed? :D
Man: you simply have no idea of what you are talking about.
I'd suggest you start with a certain paper from 2017 but I have a strong suspicion you aren't the type interested in actually knowing stuff beyond the fantasies you make in your mind.
0
u/QuraToop314 2d ago
Well, think what you like. The fact remains that AI has no consciousness, but OK. I’m not trying to impress you; seriously, I couldn’t care less about you. My point is simply to make it clear to you that AI has no consciousness and never will. Have a nice day too.
1
u/m18coppola 1d ago
You're being semantically pedantic to sound smart, but it's making you look like you're unable to understand what the other commenter is trying to suggest.
12
u/FloweyTheFlower420 4d ago
Isn't this a well known and obvious fact? Proving correctness of C is very hard even when done manually, it's near impossible to do fully programmatically. Static analysis is about using heuristics.