diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f62a3c4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.10) +project(cppcheck_test) + +set(CMAKE_CXX_STANDARD 11) + +include_directories(include) + +add_executable(cppcheck_test + src/main.cpp + src/errors.cpp + src/memory.cpp + src/utils.cpp +) \ No newline at end of file diff --git a/cppcheck_report.txt b/cppcheck_report.txt new file mode 100644 index 0000000..2a3ea25 --- /dev/null +++ b/cppcheck_report.txt @@ -0,0 +1,80 @@ +src\errors.cpp:2:2: information: Include file: not found. Please note: Standard library headers do not need to be provided to get proper results. [missingIncludeSystem] +#include + ^ +src\errors.cpp:13:21: error: Array 'arr[3]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds] + std::cout << arr[10] << std::endl; + ^ +src\errors.cpp:7:19: error: Null pointer dereference: p [nullPointer] + std::cout << *p << std::endl; + ^ +src\errors.cpp:6:14: note: Assignment 'p=nullptr', assigned value is 0 + int* p = nullptr; + ^ +src\errors.cpp:7:19: note: Null pointer dereference + std::cout << *p << std::endl; + ^ +src\errors.cpp:6:10: style: Variable 'p' can be declared as pointer to const [constVariablePointer] + int* p = nullptr; + ^ +src\errors.cpp:12:9: style: Variable 'arr' can be declared as const array [constVariable] + int arr[3] = {1,2,3}; + ^ +src\errors.cpp:19:9: error: Uninitialized variable: val [uninitvar] + if (val > 10) { + ^ +src\memory.cpp:2:2: information: Include file: not found. Please note: Standard library headers do not need to be provided to get proper results. [missingIncludeSystem] +#include + ^ +src\memory.cpp:3:2: information: Include file: not found. Please note: Standard library headers do not need to be provided to get proper results. [missingIncludeSystem] +#include + ^ +src\memory.cpp:9:1: error: Memory leak: data [memleak] +} +^ +src\memory.cpp:15:12: error: Memory pointed to by 'p' is freed twice. [doubleFree] + delete p; + ^ +src\memory.cpp:14:5: note: Memory pointed to by 'p' is freed twice. + delete p; + ^ +src\memory.cpp:15:12: note: Memory pointed to by 'p' is freed twice. + delete p; + ^ +src\memory.cpp:22:1: error: Resource leak: fp [resourceLeak] +} +^ +src\memory.cpp:7:10: style: Variable 'data' can be declared as pointer to const [constVariablePointer] + int* data = new int[100]; + ^ +src\memory.cpp:20:11: style: Variable 'fp' can be declared as pointer to const [constVariablePointer] + FILE* fp = fopen("test.txt", "w"); + ^ +src\memory.cpp:14:12: error: Memory is allocated but not initialized: p [uninitdata] + delete p; + ^ +src\memory.cpp:7:15: style: Variable 'data' is assigned a value that is never used. [unreadVariable] + int* data = new int[100]; + ^ +src\memory.cpp:7:10: style: Variable 'data' is allocated memory that is never used. [unusedAllocatedMemory] + int* data = new int[100]; + ^ +src\memory.cpp:13:10: style: Variable 'p' is allocated memory that is never used. [unusedAllocatedMemory] + int* p = new int; + ^ +src\memory.cpp:20:14: style: Variable 'fp' is assigned a value that is never used. [unreadVariable] + FILE* fp = fopen("test.txt", "w"); + ^ +src\utils.cpp:2:2: information: Include file: not found. Please note: Standard library headers do not need to be provided to get proper results. [missingIncludeSystem] +#include + ^ +src\utils.cpp:6:11: style: Variable 'a' is assigned a value that is never used. [unreadVariable] + int a = 10; + ^ +src\utils.cpp:11:11: style: Variable 'x' is assigned a value that is never used. [unreadVariable] + int x = 100; + ^ +src\utils.cpp:5:13: style: The function 'unused_function' is never used. [unusedFunction] +static void unused_function() { + ^ +nofile:0:0: information: Active checkers: 173/186 (use --checkers-report= to see details) [checkersReport] + diff --git a/cppcheck_report.xml b/cppcheck_report.xml new file mode 100644 index 0000000..efb36be --- /dev/null +++ b/cppcheck_report.xml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + p + + + + p + + + + arr + + + + val + + + + + + + + + + data + + + + + p + + + + fp + + + + data + + + + fp + + + + p + + + + data + + + + data + + + + p + + + + fp + + + + + + + a + + + + x + + + + unused_function + + + + diff --git a/cppcheck_scan.bat b/cppcheck_scan.bat new file mode 100644 index 0000000..287c9ec --- /dev/null +++ b/cppcheck_scan.bat @@ -0,0 +1,4 @@ +@echo off +cppcheck --enable=all --std=c++11 -I include src/ 2> cppcheck_report.txt +echo 扫描完成!报告已保存到 cppcheck_report.txt +pause \ No newline at end of file diff --git a/cppcheck_scan.sh b/cppcheck_scan.sh new file mode 100644 index 0000000..a8bd502 --- /dev/null +++ b/cppcheck_scan.sh @@ -0,0 +1,3 @@ +#!/bin/bash +cppcheck --enable=all --std=c++11 -I include src/ 2> cppcheck_report.txt +echo "扫描完成!报告已保存到 cppcheck_report.txt" \ No newline at end of file diff --git a/include/test_errors.h b/include/test_errors.h new file mode 100644 index 0000000..a0a0987 --- /dev/null +++ b/include/test_errors.h @@ -0,0 +1,13 @@ +#ifndef TEST_ERRORS_H +#define TEST_ERRORS_H + +// 声明各种错误函数 +void test_null_pointer(); +void test_array_out_of_bounds(); +void test_uninitialized_var(); +void test_memory_leak(); +void test_double_free(); +void test_file_leak(); +void test_unused_code(); + +#endif \ No newline at end of file diff --git a/src/errors.cpp b/src/errors.cpp new file mode 100644 index 0000000..91384f0 --- /dev/null +++ b/src/errors.cpp @@ -0,0 +1,22 @@ +#include "test_errors.h" +#include + +// 空指针解引用 +void test_null_pointer() { + int* p = nullptr; + std::cout << *p << std::endl; +} + +// 数组越界 +void test_array_out_of_bounds() { + int arr[3] = {1,2,3}; + std::cout << arr[10] << std::endl; +} + +// 未初始化变量 +void test_uninitialized_var() { + int val; + if (val > 10) { + std::cout << "val > 10" << std::endl; + } +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..cf7989a --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,14 @@ +#include "test_errors.h" + +int main() { + // 调用所有含错误的函数 + test_null_pointer(); + test_array_out_of_bounds(); + test_uninitialized_var(); + test_memory_leak(); + test_double_free(); + test_file_leak(); + test_unused_code(); + + return 0; +} \ No newline at end of file diff --git a/src/memory.cpp b/src/memory.cpp new file mode 100644 index 0000000..01cad08 --- /dev/null +++ b/src/memory.cpp @@ -0,0 +1,22 @@ +#include "test_errors.h" +#include +#include + +// 内存泄漏 +void test_memory_leak() { + int* data = new int[100]; + // 没有 delete[] +} + +// 重复释放 +void test_double_free() { + int* p = new int; + delete p; + delete p; +} + +// 文件句柄泄漏 +void test_file_leak() { + FILE* fp = fopen("test.txt", "w"); + // 没有 fclose +} \ No newline at end of file diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..505fdfc --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,14 @@ +#include "test_errors.h" +#include + +// 未使用函数 +static void unused_function() { + int a = 10; +} + +// 未使用变量 +void test_unused_code() { + int x = 100; + int y = 200; + std::cout << y << std::endl; +} \ No newline at end of file