facebook / infer

A static analyzer for Java, C, C++, and Objective-C
http://fbinfer.com/
MIT License
14.97k stars 2.02k forks source link

Infer only find part issues in Cmake project #1860

Open zhenjing opened 3 months ago

zhenjing commented 3 months ago

ENV: Infer version v1.2.0 contos 7.4

I have a Cmake project. (addressSanitizer.zip). run infer in the way:

mkdir build && cd build cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 .. infer run --compilation-database compile_commands.json

only issues of lsanSuppressed.c have found:

/home/zhenjing/test/addressSanitizer/lsanSuppressed.c:14: error: Memory Leak Memory dynamically allocated by malloc on line 14 is not freed after the last access at line 14, column 3.

  1. void FooBar() {
  2. malloc(7); ^
  3. }

/home/zhenjing/test/addressSanitizer/lsanSuppressed.c:18: error: Memory Leak Memory dynamically allocated by malloc on line 18 is not freed after the last access at line 18, column 3.

  1. void Baz() {
  2. malloc(5); ^
  3. }

If infer single file, more issues will be found. infer run -- clang -c ../classMemberHeapOverflow.cpp

/home/zhenjing/test/addressSanitizer/classMemberHeapOverflow.cpp:110: error: Uninitialized Value __param_0.tofCamera is read without initialization during the call to Test::Init().

  1. TCameraParam param;
  2. Test* test = new Test();;
  3. test->Init(param); ^
  4. return 0;

infer run -- clang -c ../useAfterFree.cpp

/home/zhenjing/test/addressSanitizer/useAfterFree.cpp:8: error: Use After Delete accessing array that was invalidated by delete[] on line 7.

  1. int *array = new int[100];
  2. delete [] array;
  3. return array[argc]; // BOOM ^
  4. } 10.

How to use infer find out all issues in Cmake project?

zhenjing commented 3 months ago

find ../ -type f ( -name ".c" -o -name ".cpp" ) | xargs infer run -- clang -c only issues of lsanSuppressed.c have found.

zhenjing commented 3 months ago

Write a python script for CMake project to find out all issues:

import json
import sys
import re
import os

num_args = len(sys.argv) - 1
# check argv
if len(sys.argv) < 2:
    print("Usage: python script.py <path_to_json_file>")
    sys.exit(1)

json_file_path = sys.argv[1]
module_name = 'DecisionCenter'
if (num_args > 2):
    module_name = sys.argv[2]

print("json file: ", json_file_path, " module name: ", module_name)

with open(json_file_path, 'r') as file:
    data = json.load(file)

for item in data:
    if 'command' in item and module_name in item['command']:
        print()

        command = item['command']
        #print("orig command: ", command)

        command = re.sub(r'^/opt/rh/devtoolset-9/root/usr/bin/cc', 'infer run -- clang', command)
        command = re.sub(r'^/opt/rh/devtoolset-9/root/usr/bin/c\+\+', 'infer run -- clang', command)

        command = re.sub(r'-o .*? -c', '-c', command)

        print(command)
        os.system(command)