Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 considered safe or benign(not such dangerous)?

like image 640
Xy Luo Avatar asked Dec 24 '22 11:12

Xy Luo


1 Answers

A unused dangling pointer is not dangerous...until the next developer uses it. You tagged the question as C and C++; I'll consider you work with C++, not C. For C++, avoid raw pointers with obscure semantics, and dangling tendencies. Using unique_ptr and shared_ptr can help you a great deal there.

Anyway, avoid dangling pointers. It costs nearly nothing to set a deleted pointer to nullptr and it can save tons of dev/debug time.

If you face the problem of dangling pointers, or at least the question associated with them, you probably have there an opportunity to apply the beautiful rule of zero: either your class manages memory (you are writing a container, a specific cool fancy pointer wrapper or the like), or you are writing a non-managing class that should not own any raw pointers.

like image 129
johan d Avatar answered Jan 05 '23 23:01

johan d