Open
Description
reproduce: https://godbolt.org/z/WEjY9rY1G
bug code:
#include <iostream>
void unsuspectingBug() {
int* ptr = new int(42);
// Perform some logical operations that don't affect ptr directly
for (int i = 0; i < 100; ++i) {
int temp = i * i; // Irrelevant computation
if (temp == 42) {
delete ptr;
}
}
// Forgetting to nullify ptr after deletion
// Logical usage without realizing it's been erased
if (ptr) {
std::cout << *ptr << std::endl; // Dereference of dangling pointer not realized due to logical oversight
}
}
int main() {
unsuspectingBug();
return 0;
}