feat: update tests

This commit is contained in:
张泊明518370910136 2024-11-03 02:02:22 -05:00
parent e47510c014
commit bda4664a09
GPG Key ID: D47306D7062CDA9D
2 changed files with 1 additions and 130 deletions

View File

@ -1 +1 @@
[{"name":"cppcheck","results":[{"score":33,"comment":"### Test results summary\n\n1. error: 8\n2. warning: 2\n3. portability: 0\n4. performance: 0\n5. style: 23\n6. information: 3\n7. debug: 0\n"}],"force_quit":false}]
[{"name":"cppcheck","results":[{"score":84,"comment":"### Test results summary\n\n1. error: 2\n2. warning: 0\n3. portability: 0\n4. performance: 0\n5. style: 6\n6. information: 0\n7. debug: 0\n"}],"force_quit":false}]

View File

@ -1,16 +1,3 @@
#include <climits>
#include <iostream>
#include <string>
// Uninitialized member warning
class BadClass {
int uninitMember;
public:
BadClass() {} // Missing member initialization
void print() { std::cout << uninitMember << std::endl; }
};
// Memory leak and resource management warnings
void memoryLeaks() {
int *ptr = new int(42);
@ -24,119 +11,3 @@ void memoryLeaks() {
delete doubleDel;
delete doubleDel;
}
// Null pointer dereferencing
void nullPointerDereference(int *ptr) {
if (ptr == nullptr) {
*ptr = 42; // Dereferencing null pointer
}
}
// Array bounds warnings
void arrayBoundsIssues() {
int arr[5];
for (int i = 0; i <= 5; i++) { // Off-by-one error
arr[i] = i;
}
// Using uninitialized array
int uninitArr[10];
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += uninitArr[i];
}
}
// Unused variables and dead code
void unusedVariables() {
int unused = 42;
std::string neverUsed = "hello";
if (true) {
int unreachable = 10;
} else {
// Dead code
std::cout << "This will never execute" << std::endl;
}
}
// Integer overflow
void integerOverflow() {
int max = INT_MAX;
max += 1; // Overflow
unsigned int i = 0;
for (i = 10; i >= 0; i--) { // Infinite loop due to unsigned underflow
std::cout << i << std::endl;
}
}
// Resource leak in exception
void resourceLeak() {
FILE *file = fopen("nonexistent.txt", "r");
// Missing fclose and no exception handling
char buffer[100];
fgets(buffer, 100, file);
}
// Uninitialized variable usage
void uninitializedVariables() {
int x;
if (x > 0) { // Using x without initialization
std::cout << "x is positive" << std::endl;
}
bool flag;
while (flag) { // Using uninitialized flag
std::cout << "Loop" << std::endl;
}
}
// Division by zero
void divisionByZero(int divisor) {
int result = 100 / divisor; // No check for zero
}
class Base {
public:
void normalFunction() {}
virtual void virtualFunction() {}
};
class Derived : public Base {
public:
void virtualFunction() {} // Missing override keyword
};
int main() {
// Memory management issues
memoryLeaks();
// Null pointer issues
int *nullPtr = nullptr;
nullPointerDereference(nullPtr);
// Array issues
arrayBoundsIssues();
// Dead code and unused variables
unusedVariables();
// Integer issues
integerOverflow();
// Resource management
resourceLeak();
// Uninitialized variables
uninitializedVariables();
// Division by zero
divisionByZero(0);
// Object slicing
Derived derived;
Base base = derived; // Object slicing
return 0;
}