From patchwork Mon Apr 11 14:42:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Schwab X-Patchwork-Id: 52777 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B2035385C339 for ; Mon, 11 Apr 2022 14:42:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B2035385C339 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1649688144; bh=6Mgzz7DnUokjqPI+rCHIGTRXJJVufi6o5oZd7lHhIdQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=MeUycQlUNbaQAEBqQgvezwi76LbZPfoyqyXY7nIDhbNwD3dnGEgu9kSEK5zGx+uJ5 u1tBibhLU5BQjKPsDRj95agodNyHKBmkvBXh9MgTmcrdZJnq92Je/Xal7eE0QuAnYi XzF82olyrqh4dIH1/hQpbuFH9gI6oFxDAR5I16+g= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 166003856DC8 for ; Mon, 11 Apr 2022 14:42:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 166003856DC8 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 2AFA0215FF for ; Mon, 11 Apr 2022 14:42:02 +0000 (UTC) Received: from hawking.suse.de (hawking.suse.de [10.160.4.0]) by relay2.suse.de (Postfix) with ESMTP id 26950A3B88 for ; Mon, 11 Apr 2022 14:42:02 +0000 (UTC) Received: by hawking.suse.de (Postfix, from userid 17005) id 139A5446BC4; Mon, 11 Apr 2022 16:42:02 +0200 (CEST) To: libc-alpha@sourceware.org Subject: [PATCH] Properly set up wide stream buffers for output (bug 20632) X-Yow: I'm encased in the lining of a pure pork sausage!! Date: Mon, 11 Apr 2022 16:42:02 +0200 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) MIME-Version: 1.0 X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andreas Schwab via Libc-alpha From: Andreas Schwab Reply-To: Andreas Schwab Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Make sure to have valid narrow buffers before allocating the buffers for a wide stream. --- libio/Makefile | 1 + libio/tst-wdoalloc.c | 32 ++++++++++++++++++++++++++++++++ libio/wfileops.c | 9 ++++----- 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 libio/tst-wdoalloc.c diff --git a/libio/Makefile b/libio/Makefile index e97387743f..3471dfa82a 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -67,6 +67,7 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc \ tst-ftell-append tst-fputws tst-bz22415 tst-fgetc-after-eof \ tst-sprintf-ub tst-sprintf-chk-ub tst-bz24051 tst-bz24153 \ tst-wfile-sync tst-bz28828 +tests += tst-wdoalloc tests-internal = tst-vtables tst-vtables-interposed diff --git a/libio/tst-wdoalloc.c b/libio/tst-wdoalloc.c new file mode 100644 index 0000000000..04cc3e906e --- /dev/null +++ b/libio/tst-wdoalloc.c @@ -0,0 +1,32 @@ +/* Test setup of buffers on wide-oriented stream - BZ #20632 */ + +#include +#include +#include +#include +#include + +static int +do_test (void) +{ + /* This test uses stderr because it is an unbuffered stream where the + stdio buffers haven't been set up yet. */ + + /* Close stderr descriptor so that output to it will fail. */ + close (2); + + /* Output a long string. */ + const int sz = 4096; + wchar_t *buff = calloc (sz + 1, sizeof *buff); + for (int i = 0; i < sz; i++) + buff[i] = L'x'; + fputws (buff, stderr); + + /* Output shorter strings. */ + for (int i = 0; i < 1024; i++) + fputws (L"0123456789ABCDEF", stderr); + + return 0; +} + +#include diff --git a/libio/wfileops.c b/libio/wfileops.c index b59a98881f..60ddf36d67 100644 --- a/libio/wfileops.c +++ b/libio/wfileops.c @@ -418,16 +418,15 @@ _IO_wfile_overflow (FILE *f, wint_t wch) /* Allocate a buffer if needed. */ if (f->_wide_data->_IO_write_base == 0) { - _IO_wdoallocbuf (f); - _IO_free_wbackup_area (f); - _IO_wsetg (f, f->_wide_data->_IO_buf_base, - f->_wide_data->_IO_buf_base, f->_wide_data->_IO_buf_base); - if (f->_IO_write_base == NULL) { _IO_doallocbuf (f); _IO_setg (f, f->_IO_buf_base, f->_IO_buf_base, f->_IO_buf_base); } + _IO_wdoallocbuf (f); + _IO_free_wbackup_area (f); + _IO_wsetg (f, f->_wide_data->_IO_buf_base, + f->_wide_data->_IO_buf_base, f->_wide_data->_IO_buf_base); } else {