Skip to content

Secure the assigned value before destroying the contained value of proxy - #114

Merged
mingxwa merged 1 commit into
ngcpp:feature/v5from
mingxwa:user/mingxwa/assign
Nov 22, 2026
Merged

mingxwa merged 1 commit into
ngcpp:feature/v5from
mingxwa:user/mingxwa/assign

Conversation

@mingxwa

@mingxwa mingxwa commented Nov 20, 2026 •

Copy link
Copy Markdown
Member
  • 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
mingxwa force-pushed the user/mingxwa/assign branch 3 times, most recently from 55e677a to aa8b848 Compare November 22, 2026 05:52
- 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
mingxwa force-pushed the user/mingxwa/assign branch from aa8b848 to 0981f72 Compare November 22, 2026 06:01
@mingxwa
mingxwa merged commit 8b8254c into ngcpp:feature/v5 Nov 22, 2026
20 checks passed
@mingxwa
mingxwa deleted the user/mingxwa/assign branch November 22, 2026 06:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant