Delete functions that return a pointer when the dynamic object is a rvalue.
Summary: This diff is not yet complete, I want to see the contbuild before I change the functions that return references to member functions. It is unsafe to return a pointer when the dynamic object is a rvalue, because if the pointer escapes the expression after the object is destroyed, we go into segfault / undefined behavior land. I have deleted these overloads. The amount of valid code that is now disallowed is minimal. The only valid case I can think of is returing a pointer and passing it to a function in the same expression that does not save the pointer. However, this case is also dangerous, because if the function you pass it to decides to save the pointer for later, we are in trouble, e.g. save_ptr(dynamic("str").c_str()) Since there are simple workarounds (naming the object), I think that is a small price to pay for the greatly increased safety. The next step is to overload all members that return a reference to a member to move the member out if the dynamic is a rvalue: const dynamic& at(dynamic const&) const&; dynamic& at(dynamic const&) &; dynamic at(dynamic const&) &&; // Move out I also need to go over the code more carefully to make sure that nothing went wrong. Reviewed By: @marcinpe Differential Revision: D2257914
Showing
Please register or sign in to comment