From patchwork Thu Oct 30 11:35:02 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Schwab X-Patchwork-Id: 405037 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id EE30414007F for ; Thu, 30 Oct 2014 22:35:24 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:mime-version :content-type; q=dns; s=default; b=uM7NEe/drGqYY3NeQdEBGIkPHoJUM Fb48pdnzRLqktl0uR38uks5214CC8ybUEcS7r/4kLfamVemLIDAoucwK9zTH6A3M 2typyi3pNWsQnkjYD3elojVLBWcSjxJfkCADVmNZ3568oJjwJFdwUPHwH/Av3Gwj lwz3PGVt1tDjs0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:mime-version :content-type; s=default; bh=F6crCl3bEG7VNjTjBOo7S4sa3VI=; b=ck5 7yfXb8rVST73SCQadYfXTaaeNkJu7CPmmrhKNOPMUvsHk2fCaV19uZ5PHLK2B4MB llu5pxKSwZsojoSBJT69m6rS1dVa/X+1lTd0UirZSOyKITI2bY4FWPGRmWDyvGa4 BNW2jhwgCfspCa6HnYXOwt/1AXdkvxZe9h/W4LEY= Received: (qmail 20627 invoked by alias); 30 Oct 2014 11:35:08 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 20515 invoked by uid 89); 30 Oct 2014 11:35:07 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.2 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mx2.suse.de From: Andreas Schwab To: libc-alpha@sourceware.org Subject: [PATCH] Don't error out writing a multibyte character to an unbuffered stream (bug 17522) X-Yow: I know th'MAMBO!! I have a TWO-TONE CHEMISTRY SET!! Date: Thu, 30 Oct 2014 12:35:02 +0100 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 An unbuffered stream uses the _shortbuf[1] member of FILE as the buffer, but code conversion may need up to MB_LEN_MAX bytes. Andreas. [BZ #17522] * libio/wfileops.c (_IO_wdo_write): If the file buffer has room for less than MB_LEN_MAX use a local buffer of that size. * libio/tst-fputws.c: New file. * libio/Makefile (tests): Add tst-fputws. --- libio/Makefile | 2 +- libio/tst-fputws.c | 39 +++++++++++++++++++++++++++++++++++++++ libio/wfileops.c | 25 ++++++++++++++++++++----- 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 libio/tst-fputws.c diff --git a/libio/Makefile b/libio/Makefile index 56952ce..2742128 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -61,7 +61,7 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc \ bug-memstream1 bug-wmemstream1 \ tst-setvbuf1 tst-popen1 tst-fgetwc bug-wsetpos tst-fseek \ tst-fwrite-error tst-ftell-partial-wide tst-ftell-active-handler \ - tst-ftell-append + tst-ftell-append tst-fputws ifeq (yes,$(build-shared)) # Add test-fopenloc only if shared library is enabled since it depends on # shared localedata objects. diff --git a/libio/tst-fputws.c b/libio/tst-fputws.c new file mode 100644 index 0000000..09f53df --- /dev/null +++ b/libio/tst-fputws.c @@ -0,0 +1,39 @@ +/* Test that we can write a multibyte character to an unbuffered stream. + Copyright (C) 2014 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +static int +do_test (void) +{ + const wchar_t str[] = L"\xbe\n"; + + setlocale (LC_ALL, "en_US.UTF-8"); + setvbuf (stdout, NULL, _IONBF, 0); + + if (fputws (str, stdout) < 0) + return 1; + + return 0; +} + +#define TEST_FUNCTION do_test () + +#include diff --git a/libio/wfileops.c b/libio/wfileops.c index c5ec5f7..6a088b1 100644 --- a/libio/wfileops.c +++ b/libio/wfileops.c @@ -75,17 +75,32 @@ _IO_wdo_write (fp, data, to_do) { enum __codecvt_result result; const wchar_t *new_data; + char mb_buf[MB_LEN_MAX]; + char *write_base, *write_ptr, *buf_end; + + if (fp->_IO_write_ptr - fp->_IO_write_base < sizeof (mb_buf)) + { + /* Make sure we have room for at least one multibyte + character. */ + write_ptr = write_base = mb_buf; + buf_end = mb_buf + sizeof (mb_buf); + } + else + { + write_ptr = fp->_IO_write_ptr; + write_base = fp->_IO_write_base; + buf_end = fp->_IO_buf_end; + } /* Now convert from the internal format into the external buffer. */ result = (*cc->__codecvt_do_out) (cc, &fp->_wide_data->_IO_state, data, data + to_do, &new_data, - fp->_IO_write_ptr, - fp->_IO_buf_end, - &fp->_IO_write_ptr); + write_ptr, + buf_end, + &write_ptr); /* Write out what we produced so far. */ - if (_IO_new_do_write (fp, fp->_IO_write_base, - fp->_IO_write_ptr - fp->_IO_write_base) == EOF) + if (_IO_new_do_write (fp, write_base, write_ptr - write_base) == EOF) /* Something went wrong. */ return WEOF;