mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Anonymousemail <noreply@anonymousemail.me>
To: musl@lists.openwall.com
Subject: [musl] Broken freopen() does not reset fwide()
Date: Tue, 16 Aug 2022 18:33:35 +0000	[thread overview]
Message-ID: <6bac31d90ea71e6151f950f1cc5e6171@anonymousemail.me> (raw)

[-- Attachment #1: Type: text/plain, Size: 1966 bytes --]

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;

[-- Attachment #2: Type: text/html, Size: 2508 bytes --]

             reply	other threads:[~2022-08-17 10:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-16 18:33 Anonymousemail [this message]
2022-08-17 15:11 ` Rich Felker
2022-08-17 22:38   ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6bac31d90ea71e6151f950f1cc5e6171@anonymousemail.me \
    --to=noreply@anonymousemail.me \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).