1. Press Turn clang
sudo apt-get install Clang
2. Write the test procedure MEMLEAK.C
#include <stdio.h> #include <stdlib.h>int main () { int *mem; Mem=malloc (sizeof (int)); if (MEM) return 1; *mem=0xdeadbeaf; Free (MEM); return 0;}
3. Run code static check
Scan-build-o memleak gcc memleak.c-o m
The results are as follows:
Memleak.c:9:5:warning:dereference of NULL pointer (loaded from variable ' mem ')
*mem=0xdeadbeaf;
^~~~
1 warning generated.
Scan-build:1 Bugs found.
4. In the current directory will survive a Memleak folder, a folder named after a date, open the folder in the browser index.html view error bug report
5. Modify MEMLEAK.C
#include <stdio.h> #include <stdlib.h>int main () { int *mem; Mem=malloc (sizeof (int)); if (mem==null) return 1; *mem=0xdeadbeaf; Free (MEM); return 0;}
6. Run Scan-build-o memleak gcc memleak.c-o m again
7. The bug disappears at this time
8. Download Open Source Cgdb http://cgdb.github.io/
9. Switch to the directory where Cgdb is located, run Scan-build-o cgdb-scan./configure--prefix= $PWD/build
10. Run Scan-build-o Cgdb-scan make
11. Open the index.html in the current directory under Cgdb-scan in the browser to view the bug report
Reference documents
Http://events.linuxfoundation.org/images/stories/pdf/lcjp2012_matsumotoh.pdf
Http://clang-analyzer.llvm.org/scan-build.html
http://www.hackhowtofaq.com/blog/static-analysis-with-llvm-clang-in-linux/