Repository navigation
Secure the assigned value before destroying the contained value of proxy - #114
Merged
Merged
Conversation
mingxwa
requested review from
guominrui,
tian-lt,
tian-lt-personal and
zhihaoy
November 20, 2026 12:49
mingxwa
force-pushed
the
user/mingxwa/assign
branch
3 times, most recently
from
November 22, 2026 05:52
55e677a to
aa8b848
Compare
- Changed the copy, move, converting and pointer assignments to construct the new value before destroying the contained value of `*this`, because the source may be owned by that value, such as in `p = std::move(p->next)`, which was undefined behavior. When `*this` does not contain a value, the new value is constructed directly in `*this` without a temporary. - Moved the temporary of the copy and pointer assignments into `*this`, unless it cannot be moved, or moving it may throw while copying it cannot. A second copy is therefore made only for a facade that forbids relocation, or to keep the `noexcept` specification of the copy assignments, and the `noexcept` specifications stay unchanged. `std::move_if_noexcept` was not used, because it copies whenever the move may throw even if the copy may throw too, and it treats a `proxy` that forbids relocation as nothrow movable through its copy constructor. - Constrained the internal move of a `proxy` like its move constructors, so an rvalue falls back to the internal copy when the facade forbids relocation or requires trivial copyability, as overload resolution does for the constructors. The converting copy assignment of a facade with trivial copyability therefore copies the temporary bytewise instead of relocating it. - Documented that the source of an assignment may be owned by the contained value of `*this`, how the contained value is initialized from the temporary, and the state of `*this` when an exception is thrown. - Changed `swap` for facades that forbid relocation to copy directly instead of through the copy assignment, so it still makes three copies. - Updated the expected operations of the move assignment and of the converting copy assignment of a facade that forbids relocation when the target holds a value, and of the copy and pointer assignments to an empty `proxy`, which no longer make a temporary. Added tests that assign from a value owned by the target for each assignment and check that the source is used before the target value is destroyed, with the value of the pointer assignment test stored inline by `make_proxy_inplace` so that the test also fails when the target value is relocated away before the source is used, tests for the copy and converting copy assignments of a facade with a non-throwing copy that forbids relocation, tests for the copy and converting copy assignments of a facade with a non-throwing copy whose relocation may throw, a test for the converting copy assignment of a facade with trivial copyability whose relocation may throw, a test for the pointer assignment from an lvalue whose copy cannot throw while its move may throw, and a static assertion for the `noexcept` specification of the pointer assignment.
mingxwa
force-pushed
the
user/mingxwa/assign
branch
from
November 22, 2026 06:01
aa8b848 to
0981f72
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
*this, because the source may be owned by that value, such as inp = std::move(p->next), which was undefined behavior. When*thisdoes not contain a value, the new value is constructed directly in*thiswithout a temporary.*this, unless it cannot be moved, or moving it may throw while copying it cannot. A second copy is therefore made only for a facade that forbids relocation, or to keep thenoexceptspecification of the copy assignments, and thenoexceptspecifications stay unchanged.std::move_if_noexceptwas not used, because it copies whenever the move may throw even if the copy may throw too, and it treats aproxythat forbids relocation as nothrow movable through its copy constructor.proxylike its move constructors, so an rvalue falls back to the internal copy when the facade forbids relocation or requires trivial copyability, as overload resolution does for the constructors. The converting copy assignment of a facade with trivial copyability therefore copies the temporary bytewise instead of relocating it.*this, how the contained value is initialized from the temporary, and the state of*thiswhen an exception is thrown.swapfor facades that forbid relocation to copy directly instead of through the copy assignment, so it still makes three copies.proxy, which no longer make a temporary. Added tests that assign from a value owned by the target for each assignment and check that the source is used before the target value is destroyed, with the value of the pointer assignment test stored inline bymake_proxy_inplaceso that the test also fails when the target value is relocated away before the source is used, tests for the copy and converting copy assignments of a facade with a non-throwing copy that forbids relocation, tests for the copy and converting copy assignments of a facade with a non-throwing copy whose relocation may throw, a test for the converting copy assignment of a facade with trivial copyability whose relocation may throw, a test for the pointer assignment from an lvalue whose copy cannot throw while its move may throw, and a static assertion for thenoexceptspecification of the pointer assignment.