Questions tagged [dangling-pointer]

Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type.

Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type. These are special cases of memory safety violations.

Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data.

Source: http://en.wikipedia.org/wiki/Dangling_pointer

167 questions
10
votes
1 answer

Is it unsafe to co_yield a pointer to a local coroutine variable?

It's common knowledge that returning a pointer to a stack variable is generally a bad idea: int* foo() { int i = 0; return &i; } int main() { int* p = foo(); } In the example above, my understanding is that the int is destroyed and so p is a…
user118534
  • 101
  • 2
10
votes
1 answer

Dangling pointer to polymorphic class leads to Undefined Behaviour. Is it true that it can be the source of any corruption imaginable?

I know that Undefined Behaviour, once it has happened, makes it impossible to think about the code any longer. I am convinced, completely. I even think I should not dig too much into understanding UB: a sane C++ program should not play with UB,…
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
10
votes
3 answers

What is the most hardened set of options for GCC compiling C/C++?

What set of GCC options provide the best protection against memory corruption vulnerabilities such as Buffer Overflows, and Dangling Pointers? Does GCC provide any type of ROP chain mitigation? Are there performance concerns or other issues that…
rook
  • 66,304
  • 38
  • 162
  • 239
9
votes
1 answer

Can dangling pointer be equal to valid pointer during constant evaluation in C++?

During constant expression evaluation in C++17, shall the compiler consider any pointer addressing a valid object unequal to any pointer addressing an object after the end of its lifetime? For example: constexpr auto f(int * x = nullptr) { int c…
Fedor
  • 17,146
  • 13
  • 40
  • 131
9
votes
1 answer

C++ return a std::string &

std::string &func(int vlu) { std::string str; str = std::to_string(vlu) + "something"; return str; } the function above is unsafe clearly. Following is another version. std::string &func(int vlu) { return std::to_string(vlu) +…
umbreLLaJYL
  • 185
  • 1
  • 10
9
votes
1 answer

Handling Dangling Pointers in Go

I have been reading Rust and Go in parallel and I see subtle differences in how both these languages deal with dangling pointers and the problems it causes. For example, here is a version in Rust: fn main() { let reference_to_nothing =…
joesan
  • 13,963
  • 27
  • 95
  • 232
9
votes
5 answers

What's the difference between delete-ing a pointer and setting it to nullptr?

Is saying delete pointer and pointer = nullptr the same? Probably not, but does the latter free up memory? What about delete pointer; pointer = nullptr / pointer = nullptr; delete pointer? Why not use that to make a safe way to delete pointers…
Accumulator
  • 873
  • 1
  • 13
  • 34
9
votes
2 answers

Simple, efficient weak pointer that is set to NULL when target memory is deallocated

Is there a simple, efficient weak/guarded pointer? I need multiple pointers to the same object that are all automatically set to NULL when the object is deleted. There is one "master" pointer that is always used to delete the object, but there can…
Leftium
  • 16,497
  • 6
  • 64
  • 99
6
votes
2 answers

Why doesn't this create a dangling reference?

I'd think that the VS2019 suggestion would create a dangling reference situation, but I tested it out, and it seems to work. What is happening here? template class Queue { inline static std::vector
ratiotile
  • 973
  • 8
  • 16
6
votes
2 answers

Is string_view really promoting use-after-free errors?

According to an article (here and there) this code is an erroneous use-after free example: #include #include #include int main() { std::string s = "Hellooooooooooooooo "; std::string_view sv = s + "World\n"; …
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
6
votes
4 answers

How can I demonstrate a zombie object in Swift?

I've read How to demonstrate memory leak and zombie objects in Xcode Instruments? but that's for objective-c. The steps don't apply. From reading here I've understood zombies are objects which are: deallocated but something pointer is still trying…
mfaani
  • 33,269
  • 19
  • 164
  • 293
6
votes
1 answer

How can I print a dangling pointer, for demonstration purposes?

I'm trying to explain to someone why they have a dangling pointer and how free actually works (and that pointers are values and thus are passed-by-value), but for that I think I need a way to print pointers that isn't "indeterminate" (as is the case…
SoniEx2
  • 1,864
  • 3
  • 27
  • 40
6
votes
2 answers

How to deal with dynamic allocation when implementing list of objects?

I had to implement a function that looked like this: MyList * sum (MyList * l1, MyList * l2) { MyList * newlist = new MyList(); //Adds two objects and place the result in a third new list return newlist; } The function took two lists…
5
votes
7 answers

Is it compulsory to initialize pointers in C++?

Is it compulsory to initialize t in the following code, before assigning value to t? Is the code correct? void swap(int *x, int *y) { int *t; *t = *x; *x = *y; *y = *t; }
Baali
  • 83
  • 3
5
votes
2 answers

Is dangling pointer dangerous if never being used?

We have already known that use-after-free vulnerabilities could cause the security problems. Since the use-after-free error is born from dangling pointer, my question is that if the dangling pointers are not being used in a program, are they…
Xy Luo
  • 103
  • 5
1
2
3
11 12