C/C++代码缺陷静态检查工具cppcheck

cppcheck介绍和安装

CppCheck是一个C/C++代码缺陷静态检查工具。静态代码检查是检查代码是否安全和健壮,是否有隐藏问题。
CppCheck只检查编译器检查不出来的bug,不检查语法错误。

CentOS在线安装命令:
yum install cppcheck

C/C++常见内存问题

(1)heap use after free 堆内存释放后继续使用

(2)stack use after return 栈内存函数返回后继续使用

(3)stack use after scope 栈内存在作用域范围外继续使用

(4)heap buffer overflow 堆内存溢出

(5)stack buffer overflow 栈内存溢出

(6)global buffer overflow 全局内存溢出

(7)allocation size too big 堆内存分配较大

(8)memory leaks 内存泄露

(9)double free 堆内存重复释放

(10)initialization order bugs 初始化命令错误

cppcheck检查内存问题

(1)heap use after free 堆内存释放后继续使用

#include <stdio.h>
int main (int argc, char* argv []) {
    int* array = new int[100];
    delete [] array;
    int num = array[0];
    return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:5:15: error: Dereferencing 'array' after it is deallocated / released [deallocuse]
    int num = array[0];
              ^
main.cpp:4:15: error: Memory is allocated but not initialized: array [uninitdata]
    delete [] array;
              ^

(2)stack use after return 栈内存函数返回后继续使用

#include <stdio.h>
int* p = NULL;
void fun() {
    int a[10];
    p = a; // 或者 p = &a[0];
}
int main() {
    fun();
    int num = p[0];
    return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:5:5: error: Non-local variable 'p' will use pointer to local variable 'a'. [danglingLifetime]
    p = a; // 或者 p = &a[0];
    ^
main.cpp:5:9: note: Array decayed to pointer here.
    p = a; // 或者 p = &a[0];
        ^
main.cpp:4:9: note: Variable created here.
    int a[10];
        ^
main.cpp:5:5: note: Non-local variable 'p' will use pointer to local variable 'a'.
    p = a; // 或者 p = &a[0];
    ^

(3)stack use after scope 栈内存在作用域范围外继续使用

#include <stdio.h>
int* p = NULL;
int main(int argc, char* argv[]) {
    {
        int a[10];
        p = &a[0];
    }
    p[0] = 1000;
    return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:6:9: error: Non-local variable 'p' will use pointer to local variable 'a'. [danglingLifetime]
        p = &a[0];
        ^
main.cpp:6:13: note: Address of variable taken here.
        p = &a[0];
            ^
main.cpp:5:13: note: Variable created here.
        int a[10];
            ^
main.cpp:6:9: note: Non-local variable 'p' will use pointer to local variable 'a'.
        p = &a[0];
        ^
main.cpp:8:5: error: Using pointer to local variable 'a' that is out of scope. [invalidLifetime]
    p[0] = 1000;
    ^
main.cpp:6:13: note: Address of variable taken here.
        p = &a[0];
            ^
main.cpp:5:13: note: Variable created here.
        int a[10];
            ^
main.cpp:8:5: note: Using pointer to local variable 'a' that is out of scope.
    p[0] = 1000;
    ^

(4)heap buffer overflow 堆内存溢出

#include <stdio.h>
int main (int argc, char** argv) {
    int* array = new int[100];
    int res = array[100];
    delete [] array;
    return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:4:15: error: Memory is allocated but not initialized: array [uninitdata]
    int res = array[100];
              ^

(5)stack buffer overflow 栈内存溢出

#include <stdio.h>
int main(int argc, char* argv[]) {
    int a[2] = {100,200};
    int b = a[2];
    return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:4:14: error: Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]
    int b = a[2];
             ^

(6)global buffer overflow 全局内存溢出

#include <stdio.h>
int array[100];
int main(int argc, char* argv[]) {
    int num = array[100];
    return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:4:20: error: Array 'array[100]' accessed at index 100, which is out of bounds. [arrayIndexOutOfBounds]
    int num = array[100];
                   ^

(7)allocation size too big 堆内存分配较大

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
    int x = 1000;
    int y = 1000;
    int* p = (int*)malloc(x * y * x * y);
    free(p);
    return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:7:37: error: Signed integer overflow for expression 'x*y*x*y'. [integerOverflow]
    int* p = (int*)malloc(x * y * x * y);
                                    ^
main.cpp:5:13: note: Assignment 'x=1000', assigned value is 1000
    int x = 1000;
            ^
main.cpp:7:37: note: Integer overflow
    int* p = (int*)malloc(x * y * x * y);
                                    ^

(8)memory leaks 内存泄露

#include <stdlib.h>
int main(int argc, char *argv[]) {
    int* ptr = (int*)malloc(10 * sizeof(int));
    int num = ptr[0];
    return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:5:5: error: Memory leak: ptr [memleak]
    return 0;
    ^
main.cpp:4:15: error: Memory is allocated but not initialized: ptr [uninitdata]
    int num = ptr[0];
              ^

(9)double free 堆内存重复释放

#include <stdio.h>
int main(int argc, char* argv[]) {
    int* p = new int[10];
    delete [] p;
    delete [] p;
    return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:5:15: error: Memory pointed to by 'p' is freed twice. [doubleFree]
    delete [] p;
              ^
main.cpp:4:5: note: Memory pointed to by 'p' is freed twice.
    delete [] p;
    ^
main.cpp:5:15: note: Memory pointed to by 'p' is freed twice.
    delete [] p;
              ^
main.cpp:4:15: error: Memory is allocated but not initialized: p [uninitdata]
    delete [] p;
              ^

(10)initialization order bugs 初始化命令错误

暂时还没有找到能够检测出来的案例。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用VS Code进行C/C++开发时,可以通过配置插件和设置来实现代码补全功能。以下是配置C/C++代码补全的步骤: 1. 安装插件:首先,在VS Code的扩展商店中搜索并安装"C/C++"插件,该插件由Microsoft提供。 2. 配置编译器:在进行C/C++开发之前,需要确保已经安装了C/C++编译器。可以选择GCC、Clang或者MSVC等编译器。安装完成后,需要将编译器的路径添加到系统环境变量中,以便VS Code能够找到它。 3. 创建工作区:在VS Code中打开一个文件夹作为工作区,或者使用已有的工作区。 4. 配置任务:在VS Code的菜单栏中选择"终端" -> "运行任务",然后选择"配置任务"。在弹出的选项中选择"C/C++: g++ build active file"(如果使用GCC编译器)或者"C/C++: clang build active file"(如果使用Clang编译器)。这将会在工作区的.vscode文件夹下生成一个tasks.json文件。 5. 配置代码补全:在VS Code的菜单栏中选择"文件" -> "首选项" -> "设置",然后在右侧的设置面板中搜索"C/C++"。找到"C/C++: Intelli Sense Engine"选项,并将其设置为"Default"。这将启用代码补全功能。 6. 配置头文件路径:如果项目中使用了自定义的头文件,需要将其路径添加到配置中,以便代码补全能够正确识别和补全这些头文件。在.vscode文件夹下创建一个名为"c_cpp_properties.json"的文件,并在其中添加以下内容: ```json { "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**", "C:/path/to/your/header/files" ], "defines": [], "compilerPath": "C:/path/to/your/compiler", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "gcc-x64" } ], "version": 4 } ``` 将上述配置中的"C:/path/to/your/header/files"替换为你的头文件所在的路径,将"C:/path/to/your/compiler"替换为你的编译器路径。 7. 重启VS Code:完成上述配置后,重启VS Code使配置生效。 现在,你应该可以在编写C/C++代码时享受到代码补全的功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值