Skip to content

Commit b48e7cc

Browse files
committed
New issue from Jonathan: unique_ptr::operator* should not allow dangling references
1 parent 476734e commit b48e7cc

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

xml/issue4148.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version='1.0' encoding='utf-8' standalone='no'?>
2+
<!DOCTYPE issue SYSTEM "lwg-issue.dtd">
3+
4+
<issue num="4148" status="New">
5+
<title><code>unique_ptr::operator*</code> should not allow dangling references</title>
6+
<section><sref ref="[unique.ptr.single.observers]"/></section>
7+
<submitter>Jonathan Wakely</submitter>
8+
<date>02 Sep 2024</date>
9+
<priority>99</priority>
10+
11+
<discussion>
12+
<p>
13+
If <code>unique_ptr&lt;T,D&gt;::element_type*</code> and <code>D::pointer</code>
14+
are not the same type, it's possible for `operator*()` to return a dangling
15+
reference that has undefined behaviour.
16+
</p>
17+
<pre><code>
18+
struct deleter {
19+
using pointer = long*;
20+
void operator()(pointer) const {}
21+
};
22+
long l = 0;
23+
std::unique_ptr&lt;const int, deleter&gt; p(&amp;l);
24+
int i = *p; <i><strong>// undefined</strong></i>
25+
</code></pre>
26+
<p>
27+
We should make this case ill-formed.
28+
</p>
29+
</discussion>
30+
31+
<resolution>
32+
<p>
33+
This wording is relative to <paper num="N4988"/>.
34+
</p>
35+
<ol>
36+
<li><p>Modify <sref ref="[unique.ptr.single.observers]"/> as indicated:</p>
37+
<blockquote>
38+
<pre>
39+
constexpr add_lvalue_reference_t&lt;T&gt; operator*() const noexcept(noexcept(*declval&lt;pointer&gt;()));
40+
</pre>
41+
<blockquote>
42+
<p>
43+
<ins>-?- <i>Mandates</i>:
44+
<code>reference_converts_from_temporary_v&lt;add_lvalue_reference_t&lt;T&gt;,
45+
decltype(*declval&lt;pointer&gt;())&gt;</code>
46+
is `false`.
47+
</ins>
48+
</p>
49+
<p>
50+
-1- <i>Preconditions</i>: `get() != nullptr` <ins>is `true`</ins>.
51+
</p>
52+
<p>
53+
-2- <i>Returns</i>: `*get()`.
54+
</p>
55+
</blockquote>
56+
</blockquote>
57+
</li>
58+
</ol>
59+
</resolution>
60+
61+
</issue>

0 commit comments

Comments
 (0)