-
Notifications
You must be signed in to change notification settings - Fork 0
add bad code #3
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
base: main
Are you sure you want to change the base?
add bad code #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,89 @@ | ||||||||||||||||||||||||||||
#include <iostream> | ||||||||||||||||||||||||||||
#include <cstring> | ||||||||||||||||||||||||||||
#include <vector> | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
using namespace std; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
int divide(int divisor) { | ||||||||||||||||||||||||||||
int dividend; | ||||||||||||||||||||||||||||
return dividend / divisor; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
void copyString() { | ||||||||||||||||||||||||||||
char buffer[10]; | ||||||||||||||||||||||||||||
strcpy(buffer, "This string is way too long!"); | ||||||||||||||||||||||||||||
cout << buffer << endl; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+12
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical buffer overflow vulnerability. The string being copied exceeds the buffer size, causing a buffer overflow. This is a serious security vulnerability. Use safer string handling: -void copyString() {
- char buffer[10];
- strcpy(buffer, "This string is way too long!");
- cout << buffer << endl;
-}
+void copyString() {
+ const char* message = "This string is way too long!";
+ char buffer[50]; // Ensure buffer is large enough
+ strncpy(buffer, message, sizeof(buffer) - 1);
+ buffer[sizeof(buffer) - 1] = '\0'; // Ensure null termination
+ cout << buffer << endl;
+} 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
void createPointer() { | ||||||||||||||||||||||||||||
int* ptr = new int[100]; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Memory leak: allocated memory is never freed. The function allocates memory but never deallocates it, causing a memory leak. Either deallocate the memory or return the pointer: -void createPointer() {
- int* ptr = new int[100];
-}
+void createPointer() {
+ int* ptr = new int[100];
+ // Use the pointer...
+ delete[] ptr; // Don't forget to deallocate
+} Or better, use smart pointers: void createPointer() {
auto ptr = std::make_unique<int[]>(100);
// Memory automatically freed when ptr goes out of scope
} 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
void deletePointer() { | ||||||||||||||||||||||||||||
int* data = new int(42); | ||||||||||||||||||||||||||||
delete data; | ||||||||||||||||||||||||||||
cout << *data << endl; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+22
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use-after-free bug: dereferencing deleted pointer. The function dereferences a pointer after deleting it, causing undefined behavior. Access the value before deletion: -void deletePointer() {
- int* data = new int(42);
- delete data;
- cout << *data << endl;
-}
+void deletePointer() {
+ int* data = new int(42);
+ cout << *data << endl; // Access before delete
+ delete data;
+} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
bool isPositive(int num) { | ||||||||||||||||||||||||||||
if (num > 0) | ||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||
else if (num <= 0) | ||||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+28
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Simplify redundant conditions. The function has redundant conditions and unreachable code. Simplify to: -bool isPositive(int num) {
- if (num > 0)
- return true;
- else if (num <= 0)
- return false;
- return false;
-}
+bool isPositive(int num) {
+ return num > 0;
+} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
void castType() { | ||||||||||||||||||||||||||||
double d = 3.1415926535; | ||||||||||||||||||||||||||||
int* pi = (int*)&d; | ||||||||||||||||||||||||||||
cout << *pi << endl; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+36
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type punning violates strict aliasing rules. Casting a double pointer to an int pointer and dereferencing it is undefined behavior. Use proper type conversion or union: -void castType() {
- double d = 3.1415926535;
- int* pi = (int*)&d;
- cout << *pi << endl;
-}
+void castType() {
+ double d = 3.1415926535;
+ int i = static_cast<int>(d); // Proper conversion
+ cout << i << endl;
+} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
int string2int(const char* str) { | ||||||||||||||||||||||||||||
return atoi(str) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+42
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix syntax error and improve string conversion. Missing semicolon and using deprecated Fix syntax and use modern C++ conversion: +#include <cstdlib> // for atoi
+
-int string2int(const char* str) {
- return atoi(str)
-}
+int string2int(const char* str) {
+ return std::atoi(str); // Add semicolon and std::
+} Better yet, use #include <string>
int string2int(const std::string& str) {
try {
return std::stoi(str);
} catch (const std::exception& e) {
// Handle error
return 0;
}
} 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
void readArray() { | ||||||||||||||||||||||||||||
int arr[5] = {1, 2, 3, 4, 5}; | ||||||||||||||||||||||||||||
for (int i = 0; i <= 5; i++) { | ||||||||||||||||||||||||||||
cout << arr[i] << endl; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+46
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Array index out of bounds. The loop accesses index 5, but the array only has indices 0-4. Fix the loop condition: -void readArray() {
- int arr[5] = {1, 2, 3, 4, 5};
- for (int i = 0; i <= 5; i++) {
- cout << arr[i] << endl;
- }
-}
+void readArray() {
+ int arr[5] = {1, 2, 3, 4, 5};
+ for (int i = 0; i < 5; i++) { // Use < instead of <=
+ cout << arr[i] << endl;
+ }
+} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
void printPointer(int* ptr) { | ||||||||||||||||||||||||||||
cout << *ptr << endl; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+53
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add null pointer check. The function dereferences the pointer without checking for null, causing a crash when called with Add null check: -void printPointer(int* ptr) {
- cout << *ptr << endl;
-}
+void printPointer(int* ptr) {
+ if (ptr != nullptr) {
+ cout << *ptr << endl;
+ } else {
+ cout << "Null pointer!" << endl;
+ }
+} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
void loopOutputs() { | ||||||||||||||||||||||||||||
for (unsigned int i = 10; i >= 0; i--) { | ||||||||||||||||||||||||||||
cout << i << endl; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+57
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Infinite loop due to unsigned integer wraparound. An unsigned int is always >= 0. When i reaches 0 and decrements, it wraps to UINT_MAX. Use signed int or change loop logic: -void loopOutputs() {
- for (unsigned int i = 10; i >= 0; i--) {
- cout << i << endl;
- }
-}
+void loopOutputs() {
+ for (int i = 10; i >= 0; i--) { // Use signed int
+ cout << i << endl;
+ }
+} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
void copyString1() { | ||||||||||||||||||||||||||||
char* str = (char*)malloc(5); | ||||||||||||||||||||||||||||
strcpy(str, "Hello"); | ||||||||||||||||||||||||||||
free(str); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+63
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Buffer overflow: insufficient memory allocation. "Hello" requires 6 bytes (including null terminator), but only 5 bytes are allocated. Allocate sufficient memory: -void copyString1() {
- char* str = (char*)malloc(5);
- strcpy(str, "Hello");
- free(str);
-}
+void copyString1() {
+ const char* message = "Hello";
+ char* str = (char*)malloc(strlen(message) + 1); // +1 for null terminator
+ if (str != nullptr) {
+ strcpy(str, message);
+ free(str);
+ }
+} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
int globalCounter = 0; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
void incrementCounter() { | ||||||||||||||||||||||||||||
globalCounter++; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
int main() { | ||||||||||||||||||||||||||||
divide(0); | ||||||||||||||||||||||||||||
copyString(); | ||||||||||||||||||||||||||||
createPointer(); | ||||||||||||||||||||||||||||
deletePointer(); | ||||||||||||||||||||||||||||
cout << isPositive(0) << endl; | ||||||||||||||||||||||||||||
castType(); | ||||||||||||||||||||||||||||
cout << string2int("abc") << endl; | ||||||||||||||||||||||||||||
readArray(); | ||||||||||||||||||||||||||||
printPointer(nullptr); | ||||||||||||||||||||||||||||
loopOutputs(); | ||||||||||||||||||||||||||||
copyString1(); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
return 0; | ||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix uninitialized variable and add division by zero check.
The function has two critical issues:
dividend
is used without initialization, causing undefined behaviordivisor = 0
Apply this fix:
🤖 Prompt for AI Agents