<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3070: path::lexically_relative causes surprising results if a filename can also be a 
root-name</title>
<meta property="og:title" content="Issue 3070: path::lexically_relative causes surprising results if a filename can also be a 
root-name">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3070.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++20">C++20</a> status.</em></p>
<h3 id="3070"><a href="lwg-defects.html#3070">3070</a>. <code>path::lexically_relative</code> causes surprising results if a filename can also be a 
<i>root-name</i></h3>
<p><b>Section:</b> 31.12.6.5.11 <a href="https://wg21.link/fs.path.gen">[fs.path.gen]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Billy O'Neal III <b>Opened:</b> 2018-02-23 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#fs.path.gen">issues</a> in [fs.path.gen].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>path::lexically_relative</code> constructs the resulting path with <code>operator/=</code>. If any of 
the filename elements from <code>*this</code> are themselves acceptable <i>root-name</i>s, <code>operator/=</code> 
will destroy any previous value, and take that <code>root_name()</code>. For example:
</p>
<blockquote><pre>
path("/a:/b:").lexically_relative("/a:/c:")
</pre></blockquote>
<p>
On a POSIX implementation, this would return <code>path("../b:")</code>, but on a Windows implementation, the 
<code>"b:"</code> element is interpreted as a <i>root-name</i>, and clobbers the entire result <code>path</code>, 
giving <code>path("b:")</code>. We should detect this problematic condition and fail (by returning <code>path()</code>).
</p>

<p><i>[2019-01-20 Reflector prioritization]</i></p>

<p>Set Priority to 2</p>

<p><i>[2019 Cologne Wednesday night]</i></p>

<p>Status to Ready</p>


<p id="res-3070"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4727">N4727</a>.
</p>

<ol>
<li><p>Change 31.12.6.5.11 <a href="https://wg21.link/fs.path.gen">[fs.path.gen]</a> as indicated:</p>
<blockquote>
<pre>
path lexically_relative(const path&amp; base) const;
</pre>
<blockquote>
<p>
-3- [&hellip;]
<p/>
-4- <i>Effects:</i> If <code>root_name() != base.root_name()</code> is <code>true</code> or 
<code>is_absolute() != base.is_absolute()</code> is <code>true</code> or <code>!has_root_directory() 
&amp;&amp; base.has_root_directory()</code> is <code>true</code> <ins>or if any <i>filename</i> 
in <code>relative_path()</code> or <code>base.relative_path()</code> can be interpreted as a 
<i>root-name</i></ins>, returns <code>path()</code>. <ins>[<i>Note:</i> On a POSIX implementation, 
no <i>filename</i> in a <i>relative-path</i> is acceptable as a <i>root-name</i> 
&mdash; <i>end note</i>]</ins> Determines
the first mismatched element of <code>*this</code> and <code>base</code> as if by:
</p>
<blockquote><pre>
auto [a, b] = mismatch(begin(), end(), base.begin(), base.end());
</pre></blockquote>
<p>
Then,
</p>
<ol style="list-style-type: none">
<li><p>(4.1) &mdash; if <code>a == end()</code> and <code>b == base.end()</code>, returns <code>path(".")</code>; 
otherwise</p></li>
<li><p>(4.2) &mdash; let <code>n</code> be the number of <i>filename</i> elements in <code>[b, base.end())</code> that 
are not dot or dot-dot minus
the number that are dot-dot. If <code>n &lt; 0</code>, returns <code>path()</code>; otherwise</p></li>
<li><p>(4.3) &mdash; returns an object of class <code>path</code> that is default-constructed, followed by</p>
<ol style="list-style-type: none">
<li><p>(4.3.1) &mdash; application of <code>operator/=(path(".."))</code> <code>n</code> times, and then</p></li>
<li><p>(4.3.2) &mdash; application of <code>operator/=</code> for each element in <code>[a, end())</code>.</p></li>
</ol>
</li>
</ol>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
