[Linux] Implements code static analysis with Clang

Source: Internet
Author: User

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/






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.