infer icon indicating copy to clipboard operation
infer copied to clipboard

Infer only find part issues in Cmake project

Open zhenjing opened this issue 1 year ago • 2 comments

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. 12. 13. void FooBar() { 14. malloc(7); ^ 15. } 16.

/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. 16. 17. void Baz() { 18. malloc(5); ^ 19. } 20.

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(). 108. TCameraParam param; 109. Test* test = new Test();; 110. test->Init(param); ^ 111. 112. 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. 6. int *array = new int[100]; 7. delete [] array; 8. return array[argc]; // BOOM ^ 9. } 10.

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

zhenjing avatar Aug 08 '24 01:08 zhenjing

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

zhenjing avatar Aug 08 '24 01:08 zhenjing

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)

zhenjing avatar Aug 12 '24 03:08 zhenjing