Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Tue, 16 Aug 2022 18:33:35 +0000
From: Anonymousemail <noreply@...nymousemail.me>
To: musl@...ts.openwall.com
Subject: Broken freopen() does not reset fwide()

I&#39;m using musl based distribution.
The official example from https://en.cppreference.com/w/c/io/fwide is malfunctioning, musl does not reset the fwide(), returns -1 (meaning BYTE oriented).

Output from example on musl:
1) A newly opened stream has no orientation.
&nbsp;&nbsp; &nbsp;no orientation
2) Establish byte orientation.
&nbsp;&nbsp; &nbsp;narrow orientation
&nbsp;&nbsp; &nbsp;narrow character read &#39;#&#39;
&nbsp;&nbsp; &nbsp;wide character read &#39;i&#39;
3) Only freopen() can reset stream orientation.
4) A reopened stream has no orientation.
&nbsp;&nbsp; &nbsp;narrow orientation&nbsp; &lt;- problem detected here, should say &quot;no orientation&quot;
5) Establish wide orientation.
&nbsp;&nbsp; &nbsp;narrow orientation
&nbsp;&nbsp; &nbsp;narrow character read &#39;#&#39;
&nbsp;&nbsp; &nbsp;wide character read &#39;i&#39;

Another simple example to reproduce the issue.:
#include &lt;stdio.h&gt;
#include &lt;wchar.h&gt;
#include &lt;stdlib.h&gt; // for EXIT_SUCCESS
#include &lt;assert.h&gt;

int main() {
&nbsp;&nbsp;&nbsp; enum { narrow = -1, query = 0, wide = 1 };
&nbsp;&nbsp;&nbsp; FILE* test = fopen(&quot;test.bin&quot;, &quot;r&quot;);
&nbsp;&nbsp;&nbsp; if(!test) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; puts(&quot;You need to have test.bin file.&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abort();
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; // establish NARROW orientation
&nbsp;&nbsp;&nbsp; fwide(test, narrow);
&nbsp;&nbsp;&nbsp; // reopen to reset, broken on musl
&nbsp;&nbsp;&nbsp; freopen(&quot;test.bin&quot;, &quot;r&quot;, test);
&nbsp;&nbsp;&nbsp; assert(fwide(test, query) == 0); // will fail on musl
&nbsp;&nbsp;&nbsp; // CLEANUP
&nbsp;&nbsp;&nbsp; fclose(test);
&nbsp;&nbsp;&nbsp; // Exit the program
&nbsp;&nbsp;&nbsp; return EXIT_SUCCESS;
}

-----
Save as main.c, run with
cc main.c
echo test &gt;test.bin
./a.out

Will output
Assertion failed: fwide(test, query) == 0 (main2.c: main: 17)
Aborted
&nbsp;

Content of type "text/html" skipped

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.