Skip to content

Commit aa8b848

Browse files
committed
Secure the assigned value before destroying the contained value of proxy
- 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, a test for the copy assignment 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, and a static assertion for the `noexcept` specification of the pointer assignment.
1 parent 55f13e2 commit aa8b848

3 files changed

Lines changed: 336 additions & 53 deletions

File tree

‎docs/spec/proxy/assignment.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ proxy& operator=(P&& ptr)
5050
F::destructibility >= lifetime_requirement::supported);
5151
```
5252
53-
Assigns a new value to `proxy` or destroys the contained value.
53+
Assigns a new value to `proxy` or destroys the contained value. For `(2-6)`, the source of the assignment may be owned by the contained value of `*this`, such as in `p = std::move(p->next)`. Therefore, if `*this` contains a value, the new value is first constructed from the source as a temporary `temp`, then the contained value is destroyed, and then the contained value is initialized from `temp` as specified below. If an exception is thrown while the new value is constructed from the source, `*this` is not modified. If an exception is thrown otherwise, `*this` does not contain a value.
5454
55-
- `(1)` Destroys the current contained value if it exists. After the call, `*this` does not contain a value.
56-
- `(2)` Copy assignment operator copies the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). When `F::copyability == lifetime_requirement::trivial`, copy assignment is defaulted. It is trivial only if the metadata storage and accessor base classes also permit trivial copy assignment.
57-
- `(3)` Move assignment operator moves the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). If the move construction throws when `F::relocatability == lifetime_requirement::supported`, `*this` does not contain a value. After move assignment, `rhs` is in a valid state with an unspecified value. The move assignment operator does not participate in overload resolution when `F::copyability == lifetime_requirement::trivial`, falling back to the defaulted copy assignment operator.
58-
- `(4)` Converting copy assignment operator copies the contained value of `rhs` to `*this`, as if by constructing a `proxy` from `rhs` and assigning it. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). Participates in overload resolution only if `F2` is not `F` and `F` is a super of `F2`, reachable via `typename F2::super_types` transitively.
59-
- `(5)` Converting move assignment operator moves the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). If the move construction throws when `F::relocatability == lifetime_requirement::supported`, `*this` does not contain a value. After the assignment, `rhs` does not contain a value. Participates in overload resolution only if `F2` is not `F` and `F` is a super of `F2`, reachable via `typename F2::super_types` transitively. The converting move assignment operator does not participate in overload resolution when `F::copyability == lifetime_requirement::trivial`, falling back to `(4)`.
60-
- `(6)` Let `VP` be `std::decay_t<P>`. Sets the contained value to an object of type `VP`, direct-non-list-initialized with `std::forward<P>(ptr)`. Participates in overload resolution only if `VP` is not a specialization of `proxy` and is a pointer-like type eligible for `proxy` (see [*ProxyFacade* requirements](../ProxyFacade.md)). *Since 3.3.0*: If [`proxiable<VP, F, MP>`](../proxiable.md) is `false`, the program is ill-formed and a diagnostic is generated.
55+
- `(1)` Destroys the current contained value if it exists. After the call, `*this` does not contain a value, even if the destruction throws.
56+
- `(2)` Copy assignment operator copies the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). If `*this` contains a value, `temp` is a `proxy`, and the contained value is initialized as if by `proxy(std::move(temp))` if `std::is_nothrow_move_constructible_v<proxy>` is `true` or `std::is_nothrow_copy_constructible_v<proxy>` is `false`, or as if by `proxy(std::as_const(temp))` otherwise. When `F::copyability == lifetime_requirement::trivial`, copy assignment is defaulted. It is trivial only if the metadata storage and accessor base classes also permit trivial copy assignment.
57+
- `(3)` Move assignment operator moves the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). If `*this` contains a value, `temp` is a `proxy`, and the contained value is initialized as if by `proxy(std::move(temp))`. If moving the contained value of `rhs` throws, `rhs` does not contain a value. After move assignment, `rhs` is in a valid state with an unspecified value. The move assignment operator does not participate in overload resolution when `F::copyability == lifetime_requirement::trivial`, falling back to the defaulted copy assignment operator.
58+
- `(4)` Converting copy assignment operator copies the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). If `*this` contains a value, `temp` is a `proxy`, and the contained value is initialized from it as in `(2)`. Participates in overload resolution only if `F2` is not `F` and `F` is a super of `F2`, reachable via `typename F2::super_types` transitively.
59+
- `(5)` Converting move assignment operator moves the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). If `*this` contains a value, `temp` is a `proxy`, and the contained value is initialized from it as in `(3)`. After the assignment, `rhs` does not contain a value, even if an exception is thrown. Participates in overload resolution only if `F2` is not `F` and `F` is a super of `F2`, reachable via `typename F2::super_types` transitively. The converting move assignment operator does not participate in overload resolution when `F::copyability == lifetime_requirement::trivial`, falling back to `(4)`.
60+
- `(6)` Let `VP` be `std::decay_t<P>`. Sets the contained value to an object of type `VP`, direct-non-list-initialized with `std::forward<P>(ptr)`. If `*this` contains a value, `temp` is an object of type `VP`, and the contained value is direct-non-list-initialized with `std::move(temp)` if `std::is_move_constructible_v<VP>` is `true` and either `std::is_nothrow_move_constructible_v<VP>` is `true` or `std::is_nothrow_copy_constructible_v<VP>` is `false`, or with `std::as_const(temp)` otherwise. Participates in overload resolution only if `VP` is not a specialization of `proxy` and is a pointer-like type eligible for `proxy` (see [*ProxyFacade* requirements](../ProxyFacade.md)). *Since 3.3.0*: If [`proxiable<VP, F, MP>`](../proxiable.md) is `false`, the program is ill-formed and a diagnostic is generated.
6161
6262
## Return Value
6363

‎include/proxy/v5/detail/core.h‎

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ template <class T, class... Args>
204204
concept nothrow_constructible =
205205
constructible<T, Args...> &&
206206
noexcept(::new (std::declval<void*>()) T(std::declval<Args>()...));
207+
template <class T>
208+
using move_or_copy_t =
209+
std::conditional_t<std::is_move_constructible_v<T> &&
210+
(std::is_nothrow_move_constructible_v<T> ||
211+
!std::is_nothrow_copy_constructible_v<T>),
212+
T&&, const T&>;
207213

208214
template <class T, lifetime_requirement LR>
209215
struct copyability_traits : inapplicable_traits {};
@@ -1255,14 +1261,11 @@ class proxy : public detail::proxy_traits<F, MP>::direct_accessor,
12551261
F::destructibility >= lifetime_requirement::supported)
12561262
{
12571263
if (this != std::addressof(rhs)) [[likely]] {
1258-
if constexpr (F::copyability == lifetime_requirement::nothrow) {
1259-
destroy();
1260-
initialize(rhs);
1261-
} else if constexpr (F::relocatability >=
1262-
lifetime_requirement::supported) {
1263-
*this = proxy{rhs};
1264-
} else {
1264+
if (meta_) {
1265+
proxy temp = rhs;
12651266
reset();
1267+
initialize(static_cast<detail::move_or_copy_t<proxy>>(temp));
1268+
} else {
12661269
initialize(rhs);
12671270
}
12681271
}
@@ -1277,8 +1280,13 @@ class proxy : public detail::proxy_traits<F, MP>::direct_accessor,
12771280
F::copyability != lifetime_requirement::trivial)
12781281
{
12791282
if (this != std::addressof(rhs)) [[likely]] {
1280-
reset();
1281-
initialize(std::move(rhs));
1283+
if (meta_) {
1284+
proxy temp = std::move(rhs);
1285+
reset();
1286+
initialize(std::move(temp));
1287+
} else {
1288+
initialize(std::move(rhs));
1289+
}
12821290
}
12831291
return *this;
12841292
}
@@ -1292,13 +1300,11 @@ class proxy : public detail::proxy_traits<F, MP>::direct_accessor,
12921300
F::copyability >= lifetime_requirement::supported &&
12931301
F::destructibility >= lifetime_requirement::supported)
12941302
{
1295-
if constexpr (F::copyability >= lifetime_requirement::nothrow) {
1296-
destroy();
1297-
initialize(rhs);
1298-
} else if constexpr (F::relocatability >= lifetime_requirement::supported) {
1299-
*this = proxy{rhs};
1300-
} else {
1303+
if (meta_) {
1304+
proxy temp = rhs;
13011305
reset();
1306+
initialize(static_cast<detail::move_or_copy_t<proxy>>(temp));
1307+
} else {
13021308
initialize(rhs);
13031309
}
13041310
return *this;
@@ -1314,8 +1320,13 @@ class proxy : public detail::proxy_traits<F, MP>::direct_accessor,
13141320
F::destructibility >= lifetime_requirement::supported &&
13151321
F::copyability != lifetime_requirement::trivial)
13161322
{
1317-
reset();
1318-
initialize(std::move(rhs));
1323+
if (meta_) {
1324+
proxy temp = std::move(rhs);
1325+
reset();
1326+
initialize(std::move(temp));
1327+
} else {
1328+
initialize(std::move(rhs));
1329+
}
13191330
return *this;
13201331
}
13211332
template <class P>
@@ -1327,15 +1338,13 @@ class proxy : public detail::proxy_traits<F, MP>::direct_accessor,
13271338
std::is_constructible_v<std::decay_t<P>, P> &&
13281339
F::destructibility >= lifetime_requirement::supported)
13291340
{
1330-
if constexpr (std::is_nothrow_constructible_v<std::decay_t<P>, P>) {
1331-
destroy();
1332-
initialize<std::decay_t<P>>(std::forward<P>(ptr));
1333-
} else if constexpr (F::relocatability >= lifetime_requirement::supported ||
1334-
F::copyability >= lifetime_requirement::nothrow) {
1335-
*this = proxy{std::forward<P>(ptr)};
1336-
} else {
1341+
using VP = std::decay_t<P>;
1342+
if (meta_) {
1343+
VP temp(std::forward<P>(ptr));
13371344
reset();
1338-
initialize<std::decay_t<P>>(std::forward<P>(ptr));
1345+
initialize<VP>(static_cast<detail::move_or_copy_t<VP>>(temp));
1346+
} else {
1347+
initialize<VP>(std::forward<P>(ptr));
13391348
}
13401349
return *this;
13411350
}
@@ -1410,14 +1419,16 @@ class proxy : public detail::proxy_traits<F, MP>::direct_accessor,
14101419
if (meta_) {
14111420
if (rhs.meta_) {
14121421
proxy temp = *this;
1413-
*this = rhs;
1414-
rhs = temp;
1422+
reset();
1423+
initialize(rhs);
1424+
rhs.reset();
1425+
rhs.initialize(temp);
14151426
} else {
1416-
rhs = *this;
1427+
rhs.initialize(*this);
14171428
reset();
14181429
}
14191430
} else if (rhs.meta_) {
1420-
*this = rhs;
1431+
initialize(rhs);
14211432
rhs.reset();
14221433
}
14231434
}
@@ -1501,7 +1512,10 @@ class proxy : public detail::proxy_traits<F, MP>::direct_accessor,
15011512
}
15021513
}
15031514
template <facade F2>
1504-
void initialize(proxy<F2, MP>&& rhs) {
1515+
void initialize(proxy<F2, MP>&& rhs)
1516+
requires(F::relocatability >= lifetime_requirement::supported &&
1517+
F::copyability != lifetime_requirement::trivial)
1518+
{
15051519
PRO5D_DEBUG(std::ignore = &pro_symbol_guard;)
15061520
if (rhs.has_value()) {
15071521
detail::proxy_helper::meta_resetting_guard<F2, MP> guard{rhs};

0 commit comments

Comments
 (0)