<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3085: char_traits::copy precondition too weak</title>
<meta property="og:title" content="Issue 3085: char_traits::copy precondition too weak">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3085.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++23">C++23</a> status.</em></p>
<h3 id="3085"><a href="lwg-defects.html#3085">3085</a>. <code>char_traits::copy</code> precondition too weak</h3>
<p><b>Section:</b> 27.2.2 <a href="https://wg21.link/char.traits.require">[char.traits.require]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2018-03-16 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>2
</p>
<p><b>View other</b> <a href="lwg-index-open.html#char.traits.require">active issues</a> in [char.traits.require].</p>
<p><b>View all other</b> <a href="lwg-index.html#char.traits.require">issues</a> in [char.traits.require].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Table 54, Character traits requirements, says that <code>char_traits::move</code> allows the ranges to overlap, 
but <code>char_traits::copy</code> requires that <code>p</code> is not in the range <code>[s, s + n)</code>. This 
appears to be an attempt to map to the requirements of <code>memmove</code> and <code>memcpy</code> respectively,
allowing those to be used to implement the functions, however the requirements for <code>copy</code> are 
weaker than those for <code>memcpy</code>. The C standard says for <code>memcpy</code> "If copying takes place 
between objects that overlap, the behavior is undefined" which is a stronger requirement than the start 
of the source range not being in the destination range.
<p/>
All of libstdc++, libc++ and VC++ simply use <code>memcpy</code> for <code>char_traits&lt;char&gt;::copy</code>, 
resulting in undefined behaviour in this example:
</p>
<blockquote><pre>
char p[] = "abc";
char* s = p + 1;
std::char_traits&lt;char&gt;::copy(s, p, 2);
assert(std::char_traits&lt;char&gt;::compare(p, "aab", 3) == 0);
</pre></blockquote>
<p>
If the intention is to allow <code>memcpy</code> as a valid implementation then the precondition is 
wrong (unfortunately nobody realized this when fixing <code>char_traits::move</code> in LWG DR <a href="lwg-defects.html#7" title="String clause minor problems (Status: TC1)">7</a><sup><a href="https://cplusplus.github.io/LWG/issue7" title="Latest snapshot">(i)</a></sup>). 
If the intention is to require <code>memmove</code> then it is strange to have separate <code>copy</code> and 
<code>move</code> functions that both use <code>memmove</code>.
<p/>
N.B. <code>std::copy</code> and <code>std::copy_backward</code> are not valid implementations of 
<code>char_traits::copy</code> either, due to different preconditions.
<p/>
Changing the precondition implicitly applies to <code>basic_string::copy</code> (27.4.3.7.7 <a href="https://wg21.link/string.copy">[string.copy]</a>), 
and <code>basic_string_view::copy</code> (27.3.3.8 <a href="https://wg21.link/string.view.ops">[string.view.ops]</a>), which are currently required 
to support partially overlapping ranges:
</p>
<blockquote><pre>
std::string s = "abc";
s.copy(s.data() + 1, s.length() - 1);
assert(s == "aab");
</pre></blockquote>

<p><i>[2018-04-03 Priority set to 2 after discussion on the reflector.]</i></p>


<p><i>[2018-08-23 Batavia Issues processing]</i></p>

<p>No consensus for direction; revisit in San Diego. Status to Open.</p>

<p><i>[2022-04-25; Daniel rebases wording on <a href="https://wg21.link/N4910" title=" Working Draft, Standard for Programming Language C++">N4910</a>]</i></p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>
This wording is relative to <a href="https://wg21.link/N4910" title=" Working Draft, Standard for Programming Language C++">N4910</a>.
</p>

<p>
Option A:
</p>
<blockquote>
<ol>
<li>
<p>Edit 27.2.2 <a href="https://wg21.link/char.traits.require">[char.traits.require]</a>, Table 75 &mdash; "Character traits requirements" [tab:char.traits.req], as indicated:</p>

<blockquote>
<table border="1">
<caption>Table 75 &mdash; Character traits requirements [tab:char.traits.req]</caption>
<tr style="text-align:center">
<th>Expression</th>
<th>Return type</th>
<th>Assertion/note<br/>pre/post-condition</th>
<th>Complexity</th>
</tr>
<tr>
<td colspan="4" align="center">
<code>[&hellip;]</code>
</td>
</tr>
<tr>
<td>
<code>X::copy(s,p,n)</code>
</td>
<td>
<code>X::char_type*</code>
</td>
<td>
<i>Preconditions:</i> <del><code>p</code> not in <code>[s,s+n)</code></del><ins>The ranges <code>[p,p+n)</code><br/>
and <code>[s,s+n)</code> do not overlap</ins>.<br/>
<i>Returns:</i> <code>s</code>.<br/>
for each <code>i</code> in <code>[0,n)</code>, performs<br/>
<code>X::assign(s[i],p[i])</code>.
</td>
<td>
linear
</td>
</tr>
<tr>
<td colspan="4" align="center">
<code>[&hellip;]</code>
</td>
</tr>
</table>

</blockquote>
</li>
</ol>
</blockquote>

<p>
Option B:
</p>
<blockquote>
<p>NAD (i.e. implementations need to be fixed, in practice <code>char_traits::copy</code> and 
<code>char_traits::move</code> might be equivalent).
</p>
</blockquote>
</blockquote>

<p><i>[Kona 2022-11-11; Move to Ready]</i></p>

<p>LWG voted for Option A (6 for, 0 against, 1 netural)</p>

<p><i>[2023-02-13 Approved at February 2023 meeting in Issaquah. Status changed: Voting &rarr; WP.]</i></p>



<p id="res-3085"><b>Proposed resolution:</b></p>
<ol>
<li>
<p>Edit 27.2.2 <a href="https://wg21.link/char.traits.require">[char.traits.require]</a>, Table 75 &mdash; "Character traits requirements" [tab:char.traits.req], as indicated:</p>

<blockquote>
<table border="1">
<caption>Table 75 &mdash; Character traits requirements [tab:char.traits.req]</caption>
<tr style="text-align:center">
<th>Expression</th>
<th>Return type</th>
<th>Assertion/note<br/>pre/post-condition</th>
<th>Complexity</th>
</tr>
<tr>
<td colspan="4" align="center">
<code>[&hellip;]</code>
</td>
</tr>
<tr>
<td>
<code>X::copy(s,p,n)</code>
</td>
<td>
<code>X::char_type*</code>
</td>
<td>
<i>Preconditions:</i> <del><code>p</code> not in <code>[s,s+n)</code></del><ins>The ranges <code>[p,p+n)</code><br/>
and <code>[s,s+n)</code> do not overlap</ins>.<br/>
<i>Returns:</i> <code>s</code>.<br/>
for each <code>i</code> in <code>[0,n)</code>, performs<br/>
<code>X::assign(s[i],p[i])</code>.
</td>
<td>
linear
</td>
</tr>
<tr>
<td colspan="4" align="center">
<code>[&hellip;]</code>
</td>
</tr>
</table>

</blockquote>
</li>
</ol>






</body>
</html>
