From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 6947 invoked from network); 17 Aug 2022 15:11:49 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 17 Aug 2022 15:11:49 -0000 Received: (qmail 13760 invoked by uid 550); 17 Aug 2022 15:11:46 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 13719 invoked from network); 17 Aug 2022 15:11:45 -0000 Date: Wed, 17 Aug 2022 11:11:31 -0400 From: Rich Felker To: musl@lists.openwall.com Message-ID: <20220817151131.GB7074@brightrain.aerifal.cx> References: <6bac31d90ea71e6151f950f1cc5e6171@anonymousemail.me> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <6bac31d90ea71e6151f950f1cc5e6171@anonymousemail.me> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] Broken freopen() does not reset fwide() 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 > #include > #include // for EXIT_SUCCESS > #include > > 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 (  etc.) making it unreadable, so I rendered the html version to text in order to be able to reply. Rich