<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2473: basic_filebuf's relation to C FILE semantics</title>
<meta property="og:title" content="Issue 2473: basic_filebuf's relation to C FILE semantics">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2473.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++17">C++17</a> status.</em></p>
<h3 id="2473"><a href="lwg-defects.html#2473">2473</a>. <code>basic_filebuf</code>'s relation to C <code>FILE</code> semantics</h3>
<p><b>Section:</b> 31.10.3.5 <a href="https://wg21.link/filebuf.virtuals">[filebuf.virtuals]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Aaron Ballman <b>Opened:</b> 2015-02-09 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#filebuf.virtuals">issues</a> in [filebuf.virtuals].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The restrictions on reading and writing a sequence controlled by an object of class 
<code>basic_filebuf&lt;charT, traits&gt;</code> are the same as for reading and writing with 
the Standard C library <code>FILE</code>s. One of the restrictions placed by C is on the behavior of 
a stream that is opened for input and output. See the C99 standard, 7.19.5.3p6 for more 
details, but the gist is that when opened in update mode, reads and writes must have an 
intervening file positioning or flushing call to not trigger UB.
</p>

<p>
31.10.3.5 <a href="https://wg21.link/filebuf.virtuals">[filebuf.virtuals]</a> p13 specifies that <code>basic_filebuf::seekoff()</code> calls 
<code>std::fseek()</code>. However, there is no mention of <code>std::fseek()</code> in 
<code>basic_filebuf::seekpos()</code>, and no mention of <code>std::fflush()</code> in
<code>basic_filebuf::sync()</code>, which seem like an oversight.
</p>

<p>
<strong>Previous resolution [SUPERSEDED]:</strong>
</p>
<blockquote class="note">
<p>This wording is relative to N4296.</p>

<ol>
<li>
<p>Change 31.10.3.5 <a href="https://wg21.link/filebuf.virtuals">[filebuf.virtuals]</a> p16 as follows [Editorial note: A footnote referring to <code>fseek</code> is not needed,
because this is already covered by the existing footnote 334]:</p>
<blockquote>
<p>
-16- Alters the file position, if possible, to correspond to the position stored in <code>sp</code> (as described below).
Altering the file position performs as follows:
</p>
<ol>
<li><p>if <code>(om &amp; ios_base::out) != 0</code>, then update the output sequence and write any unshift sequence;</p></li>
<li><p>set the file position to <code>sp</code> <ins>as if by calling <code>std::fseek(file, sp, SEEK_SET)</code></ins>;</p></li>
<li><p>if <code>(om &amp; ios_base::in) != 0</code>, then update the input sequence;</p></li>
</ol>
<p>
where <code>om</code> is the open mode passed to the last call to <code>open()</code>. The operation fails if 
<code>is_open()</code> returns false.
</p>
</blockquote>
</li>

<li>
<p>Change 31.10.3.5 <a href="https://wg21.link/filebuf.virtuals">[filebuf.virtuals]</a> p19 as follows and add a new footnote that mimics comparable footnotes in
31.10.3.4 <a href="https://wg21.link/filebuf.members">[filebuf.members]</a> and 31.10.3.5 <a href="https://wg21.link/filebuf.virtuals">[filebuf.virtuals]</a>:</p>
<blockquote>
<p>
-19- <i>Effects</i>: If a put area exists, calls <code>filebuf::overflow</code> to write the characters to the file<ins>, then
flushes the file as if by calling <code>std::fflush(file)</code> [Footnote: The function signature <code>fflush(FILE*)</code> 
is declared in <code>&lt;cstdio&gt;</code> (27.9.2).]</ins>. If a get area exists, the effect is implementation-defined.
</p>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2015-05, Lenexa]</i></p>

<p>
Aaron provides improved wording by removing the params from <code>std::fseek()</code> due to the concerns
regarding the parameters on systems where <code>fseek</code> uses 32-bit parameters.
<p/>
Second wording improvement, replacing the new one see below. It
</p>
<ol>
<li><p>drops the <code>std::</code></p></li>
<li><p>drops the footnote for <code>fflush</code></p></li>
<li><p>replaces <code>fseek</code> with <code>fsetpos</code></p></li>
</ol>

<p>
<strong>Previous resolution [SUPERSEDED]:</strong>
</p>
<blockquote class="note">
<p>This wording is relative to N4431.</p>

<ol>
<li>
<p>Change 31.10.3.5 <a href="https://wg21.link/filebuf.virtuals">[filebuf.virtuals]</a> p16 as follows [Editorial note: A footnote referring to <code>fseek</code> is not needed,
because this is already covered by the existing footnote 334]:</p>
<blockquote>
<p>
-16- Alters the file position, if possible, to correspond to the position stored in <code>sp</code> (as described below).
Altering the file position performs as follows:
</p>
<ol>
<li><p>if <code>(om &amp; ios_base::out) != 0</code>, then update the output sequence and write any unshift sequence;</p></li>
<li><p>set the file position to <code>sp</code> <ins>as if by a call to <code>std::fseek</code></ins>;</p></li>
<li><p>if <code>(om &amp; ios_base::in) != 0</code>, then update the input sequence;</p></li>
</ol>
<p>
where <code>om</code> is the open mode passed to the last call to <code>open()</code>. The operation fails if 
<code>is_open()</code> returns false.
</p>
</blockquote>
</li>

<li>
<p>Change 31.10.3.5 <a href="https://wg21.link/filebuf.virtuals">[filebuf.virtuals]</a> p19 as follows and add a new footnote that mimics comparable footnotes in
31.10.3.4 <a href="https://wg21.link/filebuf.members">[filebuf.members]</a> and 31.10.3.5 <a href="https://wg21.link/filebuf.virtuals">[filebuf.virtuals]</a>:</p>
<blockquote>
<p>
-19- <i>Effects</i>: If a put area exists, calls <code>filebuf::overflow</code> to write the characters to the file<ins>, then
flushes the file as if by calling <code>std::fflush(file)</code> [Footnote: The function signature <code>fflush(FILE*)</code> 
is declared in <code>&lt;cstdio&gt;</code> (27.9.2).]</ins>. If a get area exists, the effect is implementation-defined.
</p>
</blockquote>
</li>
</ol>
</blockquote>


<p id="res-2473"><b>Proposed resolution:</b></p>
<p>This wording is relative to N4431.</p>

<ol>
<li>
<p>Change 31.10.3.5 <a href="https://wg21.link/filebuf.virtuals">[filebuf.virtuals]</a> p16 as follows:</p>
<blockquote>
<p>
-16- Alters the file position, if possible, to correspond to the position stored in <code>sp</code> (as described below).
Altering the file position performs as follows:
</p>
<ol>
<li><p>if <code>(om &amp; ios_base::out) != 0</code>, then update the output sequence and write any unshift sequence;</p></li>
<li><p>set the file position to <code>sp</code> <ins>as if by a call to <code>fsetpos</code></ins>;</p></li>
<li><p>if <code>(om &amp; ios_base::in) != 0</code>, then update the input sequence;</p></li>
</ol>
<p>
where <code>om</code> is the open mode passed to the last call to <code>open()</code>. The operation fails if 
<code>is_open()</code> returns false.
</p>
</blockquote>
</li>

<li>
<p>Change 31.10.3.5 <a href="https://wg21.link/filebuf.virtuals">[filebuf.virtuals]</a> p19 as follows:</p>
<blockquote>
<p>
-19- <i>Effects</i>: If a put area exists, calls <code>filebuf::overflow</code> to write the characters to the file<ins>, then
flushes the file as if by calling <code>fflush(file)</code></ins>. If a get area exists, the effect is implementation-defined.
</p>
</blockquote>
</li>
</ol>





</body>
</html>
