Let's consider such example:
#include <memory>
int main() {
int x = 3;
std::shared_ptr<int> p{&x};
//std::shared_ptr<int> p = &x;
}
This program has a double-free bug (see also in action at: https://godbolt.org/z/T8eh13).
If we were to comment out the line with p{&x}
and uncomment the line below it, compilation fails, which is good and which I would expect as per https://stackoverflow.com/a/304169/1923988
The question I have is: could shared_ptr
be implemented in a way to also protect us from usages like in the line with p{&x}
(in general: usages of taking addresses of automatic variables)?
I imagine the answer is "no, because the callee sees only a pointer type, regardless of whether caller used &
or a true pointer variable", but I wonder whether there truly is some fundamental limitation that would prevent compilers from distinguishing such two cases.