scan-build did not found error on simple program with assert()
| Bugzilla Link | 30679 |
| Resolution | WONTFIX |
| Resolved on | Oct 12, 2016 15:21 |
| Version | 3.8 |
| OS | Linux |
| CC | @devincoughlin |
Extended Description
#include <stdio.h> #include <assert.h>
static void show(const char *const msg) { assert(msg != NULL); printf("msg=%s\n", msg); }
int main(void) { show("qwe"); show(NULL); show("asd"); return 0; }
==================================
scan-build finds no error in this code. But finds other errors (like possibly memory leakage when I explicitly add such error in the code.
=============
scan-build-3.8 gcc qwe.c scan-build: Using '/usr/lib/llvm-3.8/bin/clang' for static analysis scan-build: Removing directory '/tmp/scan-build-2016-10-13-000037-5639-1' because it contains no reports. scan-build: No bugs found.
=============
qwe.i: (no difference on gcc and clang except whitespaces)
...
(!)!!!!!! note noreturn and http://clang-analyzer.llvm.org/annotations.html !!!!!
69 "/usr/include/assert.h" 3 4
extern void __assert_fail (const char *__assertion, const char *__file, unsigned int __line, const char *__function) attribute ((nothrow , leaf)) attribute ((noreturn));
...
static void show(const char *const msg) { ((msg != ((void *)0)) ? (void) (0) : __assert_fail ("msg != NULL", "qwe.c", 7, PRETTY_FUNCTION))
printf("msg=%s\n", msg);
}
...
=================
This is by design. The static analyzer trusts asserts (in verification terms, it treats an assert like an "assume").
This means that rather than warning when it detects an assertion that would fire the analyzer treats the path as not feasible and prunes it.
If you don't won't the analyzer to trust asserts, you can typically analyze your program in a release configuration, which will hide the calls to assert with the preprocessor.
assigned to @tkremenek