mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Broken freopen() does not reset fwide()
@ 2022-08-16 18:33 Anonymousemail
  2022-08-17 15:11 ` Rich Felker
  0 siblings, 1 reply; 3+ messages in thread
From: Anonymousemail @ 2022-08-16 18:33 UTC (permalink / raw)
  To: musl

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

I'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.
    no orientation
2) Establish byte orientation.
    narrow orientation
    narrow character read '#'
    wide character read 'i'
3) Only freopen() can reset stream orientation.
4) A reopened stream has no orientation.
    narrow orientation  <- problem detected here, should say "no orientation"
5) Establish wide orientation.
    narrow orientation
    narrow character read '#'
    wide character read 'i'

Another simple example to reproduce the issue.:
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <assert.h>

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

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

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

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [musl] Broken freopen() does not reset fwide()
  2022-08-16 18:33 [musl] Broken freopen() does not reset fwide() Anonymousemail
@ 2022-08-17 15:11 ` Rich Felker
  2022-08-17 22:38   ` Rich Felker
  0 siblings, 1 reply; 3+ messages in thread
From: Rich Felker @ 2022-08-17 15:11 UTC (permalink / raw)
  To: musl

On Tue, Aug 16, 2022 at 06:33:35PM +0000, Anonymousemail wrote:
>    Powered by Anonymousemail → [1]Join Us!
> 
>    I'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.
>        no orientation
>    2) Establish byte orientation.
>        narrow orientation
>        narrow character read '#'
>        wide character read 'i'
>    3) Only freopen() can reset stream orientation.
>    4) A reopened stream has no orientation.
>        narrow orientation  <- problem detected here, should say "no
>    orientation"
>    5) Establish wide orientation.
>        narrow orientation
>        narrow character read '#'
>        wide character read 'i'
> 
>    Another simple example to reproduce the issue.:
>    #include <stdio.h>
>    #include <wchar.h>
>    #include <stdlib.h> // for EXIT_SUCCESS
>    #include <assert.h>
> 
>    int main() {
>        enum { narrow = -1, query = 0, wide = 1 };
>        FILE* test = fopen("test.bin", "r");
>        if(!test) {
>            puts("You need to have test.bin file.");
>            abort();
>        }
>        // establish NARROW orientation
>        fwide(test, narrow);
>        // reopen to reset, broken on musl
>        freopen("test.bin", "r", test);
>        assert(fwide(test, query) == 0); // will fail on musl
>        // CLEANUP
>        fclose(test);
>        // Exit the program
>        return EXIT_SUCCESS;
>    }
> 
>    -----
>    Save as main.c, run with
>    cc main.c
>    echo test >test.bin
>    ../a.out
> 
>    Will output
>    Assertion failed: fwide(test, query) == 0 (main2.c: main: 17)
>    Aborted

Indeed this looks like an oversight. I'll need to look at what it will
take to fix it. Naively, just f->wide = 0 before successful return in
freopen seems ok, but probably also f->locale needs to be set to 0 to
reset the encoding rule, and it might be necessary to null out some
buffer pointers to force future calls to stdio functions to reprobe
the width.

Thanks for the report.

BTW is there a way you can fix the mailer you're using? The plain text
version of your mail was not plain text, but was littered with html
entities (&nbsp; etc.) making it unreadable, so I rendered the html
version to text in order to be able to reply.

Rich

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [musl] Broken freopen() does not reset fwide()
  2022-08-17 15:11 ` Rich Felker
@ 2022-08-17 22:38   ` Rich Felker
  0 siblings, 0 replies; 3+ messages in thread
From: Rich Felker @ 2022-08-17 22:38 UTC (permalink / raw)
  To: musl

On Wed, Aug 17, 2022 at 11:11:31AM -0400, Rich Felker wrote:
> On Tue, Aug 16, 2022 at 06:33:35PM +0000, Anonymousemail wrote:
> >    Powered by Anonymousemail → [1]Join Us!
> > 
> >    I'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.
> >        no orientation
> >    2) Establish byte orientation.
> >        narrow orientation
> >        narrow character read '#'
> >        wide character read 'i'
> >    3) Only freopen() can reset stream orientation.
> >    4) A reopened stream has no orientation.
> >        narrow orientation  <- problem detected here, should say "no
> >    orientation"
> >    5) Establish wide orientation.
> >        narrow orientation
> >        narrow character read '#'
> >        wide character read 'i'
> > 
> >    Another simple example to reproduce the issue.:
> >    #include <stdio.h>
> >    #include <wchar.h>
> >    #include <stdlib.h> // for EXIT_SUCCESS
> >    #include <assert.h>
> > 
> >    int main() {
> >        enum { narrow = -1, query = 0, wide = 1 };
> >        FILE* test = fopen("test.bin", "r");
> >        if(!test) {
> >            puts("You need to have test.bin file.");
> >            abort();
> >        }
> >        // establish NARROW orientation
> >        fwide(test, narrow);
> >        // reopen to reset, broken on musl
> >        freopen("test.bin", "r", test);
> >        assert(fwide(test, query) == 0); // will fail on musl
> >        // CLEANUP
> >        fclose(test);
> >        // Exit the program
> >        return EXIT_SUCCESS;
> >    }
> > 
> >    -----
> >    Save as main.c, run with
> >    cc main.c
> >    echo test >test.bin
> >    ../a.out
> > 
> >    Will output
> >    Assertion failed: fwide(test, query) == 0 (main2.c: main: 17)
> >    Aborted
> 
> Indeed this looks like an oversight. I'll need to look at what it will
> take to fix it. Naively, just f->wide = 0 before successful return in
> freopen seems ok, but probably also f->locale needs to be set to 0 to
> reset the encoding rule, and it might be necessary to null out some
> buffer pointers to force future calls to stdio functions to reprobe
> the width.
> 
> Thanks for the report.
> 
> BTW is there a way you can fix the mailer you're using? The plain text
> version of your mail was not plain text, but was littered with html
> entities (&nbsp; etc.) making it unreadable, so I rendered the html
> version to text in order to be able to reply.

OK, fflush took care of the buffer pointers already, so just setting
f->mode and f->locale to 0 should be fine. I'll commit a fix.

Rich

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-08-17 22:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-16 18:33 [musl] Broken freopen() does not reset fwide() Anonymousemail
2022-08-17 15:11 ` Rich Felker
2022-08-17 22:38   ` Rich Felker

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).