Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer only find part issues in Cmake project #1860

Open
zhenjing opened this issue Aug 8, 2024 · 2 comments
Open

Infer only find part issues in Cmake project #1860

zhenjing opened this issue Aug 8, 2024 · 2 comments

Comments

@zhenjing
Copy link

zhenjing commented Aug 8, 2024

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
Copy link
Author

zhenjing commented Aug 8, 2024

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

@zhenjing
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant