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 29270 invoked from network); 7 Sep 2022 20:44:53 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 7 Sep 2022 20:44:53 -0000 Received: (qmail 13651 invoked by uid 550); 7 Sep 2022 20:44:50 -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 13615 invoked from network); 7 Sep 2022 20:44:50 -0000 Date: Wed, 7 Sep 2022 16:44:37 -0400 From: Rich Felker To: "Knott, Isabelle" Cc: "musl@lists.openwall.com" Message-ID: <20220907204437.GF9709@brightrain.aerifal.cx> References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="IJpNTDwzlM2Ie8A6" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] [bug] Cannot fprintf/fwprintf numbers to a wmemstream --IJpNTDwzlM2Ie8A6 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Sep 07, 2022 at 06:53:06PM +0000, Knott, Isabelle wrote: > [AMD Official Use Only - General] > > Hello, I have found that fprintf or fwprintf fails to write numbers to wmemstreams specifically, and also incorrectly reports how many characters were actually written: > > Here is some sample code that reproduces the issue > > #include > #include > #include > #include > > int main() { > wchar_t* buf; > size_t buf_size; > FILE* fd = open_wmemstream(&buf, &buf_size); > if(fd == 0) > { > return -1; > } > //int chars_written = fprintf(fd, "%d", 40); // this also doesn't work > int chars_written = fwprintf(fd, L"%d", 40); > fclose(fd); > printf("chars_written: %d\n", chars_written); > printf("buf_size: %ld\n", buf_size); > fwprintf(stdout, L"expected: \"%d\"\n", 40); > printf("actual: \"%ls\"\n", buf); > free(buf); > return 0; > } > > with musl-gcc compiles from 1.2.3: > > chars_written: 2 > buf_size: 0 > expected: "40" > actual: "" > > with glibc 2.35, using fprintf on the wmemstream fails, but fwprintf succeeds, though it won't write to stdout for some reason: > > chars_written: 2 > buf_size: 2 > actual: "40" > > Thanks! > -Isabelle Thanks! This was a really nice find. open_wmemstream intentionally makes the stream unbuffered, and thereby doesn't even look at buffered content in its write method, but doesn't take into account the fact that, per commit bd57e2b43a5b56c00a82adbde0e33e5820c81164, vfprintf installs a temporary buffer on unbuffered streams to avoid painfully slow output on unbuffered streams where it would otherwise write a byte or a field at a time rather than a whole "line" at once. The attached patch analogous to what open_memstream does should fix it. I'll commit soon after double-checking that it's right. Thanks for the clear test case. Rich --IJpNTDwzlM2Ie8A6 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="fix_wmemstream.diff" diff --git a/src/stdio/open_wmemstream.c b/src/stdio/open_wmemstream.c index ed1b561d..b8ae4a79 100644 --- a/src/stdio/open_wmemstream.c +++ b/src/stdio/open_wmemstream.c @@ -40,8 +40,12 @@ fail: static size_t wms_write(FILE *f, const unsigned char *buf, size_t len) { struct cookie *c = f->cookie; - size_t len2; + size_t len2 = f->wpos - f->wbase; wchar_t *newbuf; + if (len2) { + f->wpos = f->wbase; + if (wms_write(f, f->wbase, len2) < len2) return 0; + } if (len + c->pos >= c->space) { len2 = 2*c->space+1 | c->pos+len+1; if (len2 > SSIZE_MAX/4) return 0; --IJpNTDwzlM2Ie8A6--