4 ms·
> Now the answer is that unique_ptr will do it when the pointer itself goes out of scope. And my claim is that this doesn't effectively reduce UAF. It doesn't
by pcwalton 11y ago
> Now the answer is that unique_ptr will do it when the pointer itself goes out of scope.
And my claim is that this doesn't effectively reduce UAF. It doesn't eliminate dangling references, etc.
- AnthonyMouse 11y agoIt seems like you're making the perfect the enemy of the good. Look at my example above. The problem isn't just exception safety, the first class is five kinds of catastrophe waiting to happen. If the user calls reinit() then init() may delete the pointer (because it was written expecting to be called during construction). So now the object exists but the pointer is invalid and any further use of the object will be UAF. Even if the caller understands the exception to mean that the object should not be used anymore, the ~fail() destructor is going to call the ~foo() destructor again and double free anyway. On top of that, the default copy and move constructors and assignment operators for a naked pointer just copy the pointer, which produces UAF as soon as the first copy to be destroyed frees the pointer the others still hold. None of that happens with the unique_ptr version. No explicit call to delete is required so we lose that opportunity to accidentally call it before the surrounding object ceases to exist. The default move constructor actually works and the default copy and assignment operators are deleted, so attempting to copy without an explicit deep copy implementation becomes a compile error instead of runtime UAF. And it's less code too. It can't actually stop you from having dangling references because if it did then it wouldn't be able to compile existing code anymore. But how is it not an improvement over the status quo?