This commit is contained in:
lids 2026-04-08 15:38:04 +08:00
parent 637de72242
commit fa2a0dee39
10 changed files with 277 additions and 0 deletions

13
CMakeLists.txt Normal file
View File

@ -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
)

80
cppcheck_report.txt Normal file
View File

@ -0,0 +1,80 @@
src\errors.cpp:2:2: information: Include file: <iostream> not found. Please note: Standard library headers do not need to be provided to get proper results. [missingIncludeSystem]
#include <iostream>
^
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: <cstdio> not found. Please note: Standard library headers do not need to be provided to get proper results. [missingIncludeSystem]
#include <cstdio>
^
src\memory.cpp:3:2: information: Include file: <cstdlib> not found. Please note: Standard library headers do not need to be provided to get proper results. [missingIncludeSystem]
#include <cstdlib>
^
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: <iostream> not found. Please note: Standard library headers do not need to be provided to get proper results. [missingIncludeSystem]
#include <iostream>
^
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=<filename> to see details) [checkersReport]

92
cppcheck_report.xml Normal file
View File

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<results version="2">
<cppcheck version="2.20.0"/>
<errors>
<error id="missingIncludeSystem" severity="information" msg="Include file: &lt;iostream&gt; not found. Please note: Standard library headers do not need to be provided to get proper results." verbose="Include file: &lt;iostream&gt; not found. Please note: Standard library headers do not need to be provided to get proper results.">
<location file="src/errors.cpp" line="2" column="2"/>
</error>
<error id="arrayIndexOutOfBounds" severity="error" msg="Array &apos;arr[3]&apos; accessed at index 10, which is out of bounds." verbose="Array &apos;arr[3]&apos; accessed at index 10, which is out of bounds." cwe="788" file0="src/errors.cpp">
<location file="src/errors.cpp" line="13" column="21" info="Array index out of bounds"/>
</error>
<error id="nullPointer" severity="error" msg="Null pointer dereference: p" verbose="Null pointer dereference: p" cwe="476" file0="src/errors.cpp">
<location file="src/errors.cpp" line="7" column="19" info="Null pointer dereference"/>
<location file="src/errors.cpp" line="6" column="14" info="Assignment &apos;p=nullptr&apos;, assigned value is 0"/>
<symbol>p</symbol>
</error>
<error id="constVariablePointer" severity="style" msg="Variable &apos;p&apos; can be declared as pointer to const" verbose="Variable &apos;p&apos; can be declared as pointer to const" cwe="398" file0="src/errors.cpp">
<location file="src/errors.cpp" line="6" column="10" info="Variable &apos;p&apos; can be declared as pointer to const"/>
<symbol>p</symbol>
</error>
<error id="constVariable" severity="style" msg="Variable &apos;arr&apos; can be declared as const array" verbose="Variable &apos;arr&apos; can be declared as const array" cwe="398" file0="src/errors.cpp">
<location file="src/errors.cpp" line="12" column="9" info="Variable &apos;arr&apos; can be declared as const array"/>
<symbol>arr</symbol>
</error>
<error id="uninitvar" severity="error" msg="Uninitialized variable: val" verbose="Uninitialized variable: val" cwe="457" file0="src/errors.cpp">
<location file="src/errors.cpp" line="19" column="9"/>
<symbol>val</symbol>
</error>
<error id="missingIncludeSystem" severity="information" msg="Include file: &lt;cstdio&gt; not found. Please note: Standard library headers do not need to be provided to get proper results." verbose="Include file: &lt;cstdio&gt; not found. Please note: Standard library headers do not need to be provided to get proper results.">
<location file="src/memory.cpp" line="2" column="2"/>
</error>
<error id="missingIncludeSystem" severity="information" msg="Include file: &lt;cstdlib&gt; not found. Please note: Standard library headers do not need to be provided to get proper results." verbose="Include file: &lt;cstdlib&gt; not found. Please note: Standard library headers do not need to be provided to get proper results.">
<location file="src/memory.cpp" line="3" column="2"/>
</error>
<error id="memleak" severity="error" msg="Memory leak: data" verbose="Memory leak: data" cwe="401" file0="src/memory.cpp">
<location file="src/memory.cpp" line="9" column="1"/>
<symbol>data</symbol>
</error>
<error id="doubleFree" severity="error" msg="Memory pointed to by &apos;p&apos; is freed twice." verbose="Memory pointed to by &apos;p&apos; is freed twice." cwe="415" file0="src/memory.cpp">
<location file="src/memory.cpp" line="15" column="12"/>
<location file="src/memory.cpp" line="14" column="5"/>
<symbol>p</symbol>
</error>
<error id="resourceLeak" severity="error" msg="Resource leak: fp" verbose="Resource leak: fp" cwe="775" file0="src/memory.cpp">
<location file="src/memory.cpp" line="22" column="1"/>
<symbol>fp</symbol>
</error>
<error id="constVariablePointer" severity="style" msg="Variable &apos;data&apos; can be declared as pointer to const" verbose="Variable &apos;data&apos; can be declared as pointer to const" cwe="398" file0="src/memory.cpp">
<location file="src/memory.cpp" line="7" column="10" info="Variable &apos;data&apos; can be declared as pointer to const"/>
<symbol>data</symbol>
</error>
<error id="constVariablePointer" severity="style" msg="Variable &apos;fp&apos; can be declared as pointer to const" verbose="Variable &apos;fp&apos; can be declared as pointer to const" cwe="398" file0="src/memory.cpp">
<location file="src/memory.cpp" line="20" column="11" info="Variable &apos;fp&apos; can be declared as pointer to const"/>
<symbol>fp</symbol>
</error>
<error id="uninitdata" severity="error" msg="Memory is allocated but not initialized: p" verbose="Memory is allocated but not initialized: p" cwe="457" file0="src/memory.cpp">
<location file="src/memory.cpp" line="14" column="12"/>
<symbol>p</symbol>
</error>
<error id="unreadVariable" severity="style" msg="Variable &apos;data&apos; is assigned a value that is never used." verbose="Variable &apos;data&apos; is assigned a value that is never used." cwe="563" file0="src/memory.cpp">
<location file="src/memory.cpp" line="7" column="15"/>
<symbol>data</symbol>
</error>
<error id="unusedAllocatedMemory" severity="style" msg="Variable &apos;data&apos; is allocated memory that is never used." verbose="Variable &apos;data&apos; is allocated memory that is never used." cwe="563" file0="src/memory.cpp">
<location file="src/memory.cpp" line="7" column="10"/>
<symbol>data</symbol>
</error>
<error id="unusedAllocatedMemory" severity="style" msg="Variable &apos;p&apos; is allocated memory that is never used." verbose="Variable &apos;p&apos; is allocated memory that is never used." cwe="563" file0="src/memory.cpp">
<location file="src/memory.cpp" line="13" column="10"/>
<symbol>p</symbol>
</error>
<error id="unreadVariable" severity="style" msg="Variable &apos;fp&apos; is assigned a value that is never used." verbose="Variable &apos;fp&apos; is assigned a value that is never used." cwe="563" file0="src/memory.cpp">
<location file="src/memory.cpp" line="20" column="14"/>
<symbol>fp</symbol>
</error>
<error id="missingIncludeSystem" severity="information" msg="Include file: &lt;iostream&gt; not found. Please note: Standard library headers do not need to be provided to get proper results." verbose="Include file: &lt;iostream&gt; not found. Please note: Standard library headers do not need to be provided to get proper results.">
<location file="src/utils.cpp" line="2" column="2"/>
</error>
<error id="unreadVariable" severity="style" msg="Variable &apos;a&apos; is assigned a value that is never used." verbose="Variable &apos;a&apos; is assigned a value that is never used." cwe="563" file0="src/utils.cpp">
<location file="src/utils.cpp" line="6" column="11"/>
<symbol>a</symbol>
</error>
<error id="unreadVariable" severity="style" msg="Variable &apos;x&apos; is assigned a value that is never used." verbose="Variable &apos;x&apos; is assigned a value that is never used." cwe="563" file0="src/utils.cpp">
<location file="src/utils.cpp" line="11" column="11"/>
<symbol>x</symbol>
</error>
<error id="unusedFunction" severity="style" msg="The function &apos;unused_function&apos; is never used." verbose="The function &apos;unused_function&apos; is never used." cwe="561">
<location file="src/utils.cpp" line="5" column="13"/>
<symbol>unused_function</symbol>
</error>
<error id="checkersReport" severity="information" msg="Active checkers: 173/186 (use --checkers-report=&lt;filename&gt; to see details)" verbose="Active checkers: 173/186 (use --checkers-report=&lt;filename&gt; to see details)"/>
</errors>
</results>

4
cppcheck_scan.bat Normal file
View File

@ -0,0 +1,4 @@
@echo off
cppcheck --enable=all --std=c++11 -I include src/ 2> cppcheck_report.txt
echo 扫描完成!报告已保存到 cppcheck_report.txt
pause

3
cppcheck_scan.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/bash
cppcheck --enable=all --std=c++11 -I include src/ 2> cppcheck_report.txt
echo "扫描完成!报告已保存到 cppcheck_report.txt"

13
include/test_errors.h Normal file
View File

@ -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

22
src/errors.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "test_errors.h"
#include <iostream>
// 空指针解引用
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;
}
}

14
src/main.cpp Normal file
View File

@ -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;
}

22
src/memory.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "test_errors.h"
#include <cstdio>
#include <cstdlib>
// 内存泄漏
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
}

14
src/utils.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "test_errors.h"
#include <iostream>
// 未使用函数
static void unused_function() {
int a = 10;
}
// 未使用变量
void test_unused_code() {
int x = 100;
int y = 200;
std::cout << y << std::endl;
}