From patchwork Tue Aug 8 13:43:11 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 109660 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp2967306qge; Tue, 8 Aug 2017 06:43:37 -0700 (PDT) X-Received: by 10.101.89.3 with SMTP id f3mr4053495pgu.177.1502199817905; Tue, 08 Aug 2017 06:43:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502199817; cv=none; d=google.com; s=arc-20160816; b=q+4VrJQe0N5BFcoHBgj2aGmikHfqnie2o0uZP4gzpxOEuJP0WU5GsZeoW9i8r+zpRl xzXsSe//RZuHWooFbC3mYbXpIEcRFs7NJvERI+2WCd75aNIrxTyYnb8S9lZ6J2GcIMBU 2IiXv6NupGQiUXwyyKDk0arHkcwDTNS5/z5dFXQFUTSZzjD0iojrmhklRVppuK1z2oVF 7Lw4gxeXShS1vnP2ehwC313d75v6mO5lXojk85nV+/fxGmn3jKx6RrY38OqgGY1Z2V09 ymRJUFIbUHAzN6rWr8Tnn+/GDQYupWvTpmWNoHRwWOL1yF8q7komAujePanb/O7Dsjct U3YQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:to:from:delivered-to:sender:list-help :list-post:list-archive:list-subscribe:list-unsubscribe:list-id :precedence:mailing-list:dkim-signature:domainkey-signature :arc-authentication-results; bh=Yg+u/+T7nTuwNn3h4GWyyXp7+yoZSR2+g/QoyM5bWU4=; b=BABU60FinXQBEd78w5PoIZ7F5jH7BP9iIe4hfb3zghaUDOdwllYcClCREz3ij/R5j+ 3b2h/Q9uRHRFLVWGCmjctTl5WeZ8WWyY3d64u3YZW6qag3TVRVMyrJM0zMrlAkfXcynm QhIPLpJQrJ0GypRh+pkhLhoJVzysNLwwzHI4Nj7DqcsMYL/0FlgLxGSu3PKHuR9FW/Us 9ioIWS/yv/S8eg+PkxKCM16/n7iFTKNSSTSE4F/eDVJJLw6j5AatF1K0jxJwxlhWMVQu KmkYipcW703yPB4rVpXdccZRS4u2ziyfkxKolTGDTyeOAGQ0c6titsoEhMMga+tGWE2Y bpVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=eW0QKCI5; spf=pass (google.com: domain of libc-alpha-return-82852-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-82852-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id r10si943766plj.678.2017.08.08.06.43.37 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Aug 2017 06:43:37 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-82852-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=eW0QKCI5; spf=pass (google.com: domain of libc-alpha-return-82852-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-82852-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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; q=dns; s= default; b=BPDplitxmL+U2EhrDCE8uQH4GBbRqmvFi5FZxw4hNHrGeQRsXnVag u7HYnJBOMhXaTpmsshFbud366Yd2/O4mzq/Vvf2UNbV10A4qxRrsr+1fwn5Zbw1z dYj/mOdKGyeYYOfS6i61KnM2Seq1CjB2PERF0D/VtskkqyNGJnb3u0= 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; s=default; bh=kzzFhScA7/kxPjxAagHFqyoPyEE=; b=eW0QKCI5MjoQ9dcpnxYQZI3++TmC ze7PvSlPckhglrfXxgM65f6e+Aqbq/kbHJIRmfZWMz1M37LNzEOi5PPpRQB8cSaY CKZ+LDS98mkMECGnN5qgy8Oo6OFwkgELz2dBBIQVaOQPPXvfuQUrT6Et8UzhEqtm 5R0M7Yq1T1SSnlw= Received: (qmail 12641 invoked by alias); 8 Aug 2017 13:43:26 -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 12620 invoked by uid 89); 8 Aug 2017 13:43:24 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=1403 X-HELO: mail-vk0-f43.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=Yg+u/+T7nTuwNn3h4GWyyXp7+yoZSR2+g/QoyM5bWU4=; b=HjsO0tGs5eWv2RA6tFQugvF7fXj61eMQTVeSMLD9bgns1xT8fxe/WhTXt6QmKwk8ds bomLy6cltzTkEFLCjzE9aP8IYzC20CGXIdpjGbCu1WxeFEf+hGzj59gTaXxG7fzfu9Qi lyUcy9sqHaF5unzyF7zLqP1Q+OBjw+11rdOHR4CL2CQ+Hwq4KMAfjOtfJ5tFzZSi8ctx /pOAPdQvonHov5U5DahLPvcs+cNJLC0wTHsU2IL44EKqoF5xgcm2bMqg5SIt77pqDrN+ 84DE/unk3dE1nuVJ/oRVn9bTblA25CYMXNool1RcTcWheer4a+OzE/tWAqjSp30KGfMy 8hzA== X-Gm-Message-State: AHYfb5jcdVkjDkmKhmryLWBFPmsNwPicm0tb3PASMxGrJ13K63O7wC5B F4NG05FP3/N9+vp57OV0fQ== X-Received: by 10.31.83.132 with SMTP id h126mr2390355vkb.131.1502199796432; Tue, 08 Aug 2017 06:43:16 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2] libio: Fix open_memstream fflush (NULL) (BZ#21735) Date: Tue, 8 Aug 2017 10:43:11 -0300 Message-Id: <1502199791-22458-1-git-send-email-adhemerval.zanella@linaro.org> POSIX specifies that the state and size after a fflush on a stream opened by open_memstream should be updated and current implementation does not act accordingly on a fflush (NULL) (meant to act on all opened streams). This patch fixes it for open_{w}memstream. Checked on x86_64-linux-gnu. [BZ #21735] * libio/memstream.c (_IO_mem_overflow): New function. (_IO_mem_jumps): User _IO_mem_overflow instead of _IO_str_overflow. (__open_memstream): Call _IO_link_in on the internal FILE object. * libio/wmemstream.c (_IO_wmem_overflow): New function. (_IO_wmem_jumps): User _IO_wmem_overflow instead of _IO_wstr_overflow. (__open_wmemstream): Call _IO_link_in on the internal FILE object. * libio/Makefile (test): Add tst-memstream4 and tst-wmemstream4. * libio/tst-memstream4.c: New file. * libio/tst-wmemstream4.c: Likewise. * libio/tst-memstream.h: Likewise. --- ChangeLog | 12 ++++++ libio/Makefile | 4 +- libio/memstream.c | 21 +++++++++-- libio/tst-memstream.h | 66 +++++++++++++++++++++++++++++++++ libio/tst-memstream4.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ libio/tst-wmemstream4.c | 20 ++++++++++ libio/wmemstream.c | 22 +++++++++-- 7 files changed, 234 insertions(+), 10 deletions(-) create mode 100644 libio/tst-memstream.h create mode 100644 libio/tst-memstream4.c create mode 100644 libio/tst-wmemstream4.c -- 2.7.4 diff --git a/libio/Makefile b/libio/Makefile index 9d09bd8..492532f 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -57,8 +57,8 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc \ tst-mmap-eofsync tst-mmap-fflushsync bug-mmap-fflush \ tst-mmap2-eofsync tst-mmap-offend bug-fopena+ bug-wfflush \ bug-ungetc2 bug-ftell bug-ungetc3 bug-ungetc4 tst-fopenloc2 \ - tst-memstream1 tst-memstream2 tst-memstream3 \ - tst-wmemstream1 tst-wmemstream2 tst-wmemstream3 \ + tst-memstream1 tst-memstream2 tst-memstream3 tst-memstream4 \ + tst-wmemstream1 tst-wmemstream2 tst-wmemstream3 tst-wmemstream4 \ 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 \ diff --git a/libio/memstream.c b/libio/memstream.c index e391efd..67ecc09 100644 --- a/libio/memstream.c +++ b/libio/memstream.c @@ -31,13 +31,14 @@ struct _IO_FILE_memstream static int _IO_mem_sync (_IO_FILE* fp) __THROW; static void _IO_mem_finish (_IO_FILE* fp, int) __THROW; +static int _IO_mem_overflow (_IO_FILE *fp, int c) __THROW; static const struct _IO_jump_t _IO_mem_jumps libio_vtable = { JUMP_INIT_DUMMY, JUMP_INIT (finish, _IO_mem_finish), - JUMP_INIT (overflow, _IO_str_overflow), + JUMP_INIT (overflow, _IO_mem_overflow), JUMP_INIT (underflow, _IO_str_underflow), JUMP_INIT (uflow, _IO_default_uflow), JUMP_INIT (pbackfail, _IO_str_pbackfail), @@ -87,6 +88,7 @@ __open_memstream (char **bufloc, _IO_size_t *sizeloc) return NULL; } _IO_init_internal (&new_f->fp._sf._sbf._f, 0); + _IO_link_in ((struct _IO_FILE_plus *) &new_f->fp._sf._sbf); _IO_JUMPS_FILE_plus (&new_f->fp._sf._sbf) = &_IO_mem_jumps; _IO_str_init_static_internal (&new_f->fp._sf, buf, _IO_BUFSIZ, buf); new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF; @@ -96,9 +98,6 @@ __open_memstream (char **bufloc, _IO_size_t *sizeloc) new_f->fp.bufloc = bufloc; new_f->fp.sizeloc = sizeloc; - /* Disable single thread optimization. BZ 21735. */ - new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK; - return (_IO_FILE *) &new_f->fp._sf._sbf; } libc_hidden_def (__open_memstream) @@ -140,3 +139,17 @@ _IO_mem_finish (_IO_FILE *fp, int dummy) _IO_str_finish (fp, 0); } + +static int +_IO_mem_overflow (_IO_FILE *fp, int c) +{ + if (c == EOF) + { + /* Updates the returned size location on stream flush. */ + struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp; + *mp->bufloc = fp->_IO_write_base; + *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base; + return 0; + } + return _IO_str_overflow (fp, c); +} diff --git a/libio/tst-memstream.h b/libio/tst-memstream.h new file mode 100644 index 0000000..ffc2a65 --- /dev/null +++ b/libio/tst-memstream.h @@ -0,0 +1,66 @@ +/* Common definitions for open_memstream tests. + Copyright (C) 2017 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 +#include +#include +#include + +#include + +#ifdef TEST_WCHAR +# include + +/* Straighforward implementation so tst-memstream3 could use check + fwrite on open_memstream. */ +static size_t __attribute__ ((used)) +fwwrite (const void *ptr, size_t size, size_t nmemb, FILE *arq) +{ + const wchar_t *wcs = (const wchar_t*) (ptr); + for (size_t s = 0; s < size; s++) + { + for (size_t n = 0; n < nmemb; n++) + if (fputwc (wcs[n], arq) == WEOF) + return n; + } + return size * nmemb; +} + +# define CHAR_T wchar_t +# define W(o) L##o +# define OPEN_MEMSTREAM open_wmemstream +# define PRINTF wprintf +# define FWRITE fwwrite +# define FPUTC fputwc +# define STRCMP wcscmp +# define STRLEN wcslen +#else +# define CHAR_T char +# define W(o) o +# define OPEN_MEMSTREAM open_memstream +# define PRINTF printf +# define FWRITE fwrite +# define FPUTC fputc +# define STRCMP strcmp +# define STRLEN strlen +#endif + +#define S(s) S1 (s) +#define S1(s) #s diff --git a/libio/tst-memstream4.c b/libio/tst-memstream4.c new file mode 100644 index 0000000..e98ca51 --- /dev/null +++ b/libio/tst-memstream4.c @@ -0,0 +1,99 @@ +/* Test for open_memstream BZ #21735. + Copyright (C) 2017 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 "tst-memstream.h" + +static void +mcheck_abort (enum mcheck_status ev) +{ + printf ("mecheck failed with status %d\n", (int) ev); + exit (1); +} + +static int +do_test (void) +{ + mcheck_pedantic (mcheck_abort); + + CHAR_T *buf; + size_t size; + + FILE *fp = OPEN_MEMSTREAM (&buf, &size); + if (fp == NULL) + FAIL_RET ("%s failed", S(OPEN_MEMSTREAM)); + + /* Basic test to check if 'fflush (NULL)' flushed all stream including + open_memstream ones. */ + { + if (FPUTC (W('a'), fp) != W('a')) + FAIL_RET ("%s failed: %m", S(FPUTC)); + + if (fflush (NULL) != 0) + FAIL_RET ("fflush failed: %m"); + + if (size != 1) + FAIL_RET ("size != 1"); + + TEST_VERIFY (STRCMP (buf, W("a")) == 0); + } + + /* Same as before now with a buf close and equal to BUFSIZ. */ + { + const size_t bufsize = BUFSIZ - 1; + CHAR_T bufcmp[bufsize + 2]; + size_t i; + + bufcmp[0] = W('a'); + for (i=1; i diff --git a/libio/tst-wmemstream4.c b/libio/tst-wmemstream4.c new file mode 100644 index 0000000..9dbf615 --- /dev/null +++ b/libio/tst-wmemstream4.c @@ -0,0 +1,20 @@ +/* Test for open_wmemstream BZ #21735. + Copyright (C) 2017 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 + . */ + +#define TEST_WCHAR +#include diff --git a/libio/wmemstream.c b/libio/wmemstream.c index 103a760..d68334b 100644 --- a/libio/wmemstream.c +++ b/libio/wmemstream.c @@ -32,13 +32,14 @@ struct _IO_FILE_wmemstream static int _IO_wmem_sync (_IO_FILE* fp) __THROW; static void _IO_wmem_finish (_IO_FILE* fp, int) __THROW; +static int _IO_wmem_overflow (_IO_FILE *fp, int c) __THROW; static const struct _IO_jump_t _IO_wmem_jumps libio_vtable = { JUMP_INIT_DUMMY, JUMP_INIT (finish, _IO_wmem_finish), - JUMP_INIT (overflow, (_IO_overflow_t) _IO_wstr_overflow), + JUMP_INIT (overflow, (_IO_overflow_t) _IO_wmem_overflow), JUMP_INIT (underflow, (_IO_underflow_t) _IO_wstr_underflow), JUMP_INIT (uflow, (_IO_underflow_t) _IO_wdefault_uflow), JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail), @@ -89,6 +90,7 @@ open_wmemstream (wchar_t **bufloc, _IO_size_t *sizeloc) } _IO_no_init (&new_f->fp._sf._sbf._f, 0, 0, &new_f->wd, &_IO_wmem_jumps); _IO_fwide (&new_f->fp._sf._sbf._f, 1); + _IO_link_in ((struct _IO_FILE_plus *) &new_f->fp._sf._sbf); _IO_wstr_init_static (&new_f->fp._sf._sbf._f, buf, _IO_BUFSIZ / sizeof (wchar_t), buf); new_f->fp._sf._sbf._f._flags2 &= ~_IO_FLAGS2_USER_WBUF; @@ -98,9 +100,6 @@ open_wmemstream (wchar_t **bufloc, _IO_size_t *sizeloc) new_f->fp.bufloc = bufloc; new_f->fp.sizeloc = sizeloc; - /* Disable single thread optimization. BZ 21735. */ - new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK; - return (_IO_FILE *) &new_f->fp._sf._sbf; } @@ -145,3 +144,18 @@ _IO_wmem_finish (_IO_FILE *fp, int dummy) _IO_wstr_finish (fp, 0); } + +static int +_IO_wmem_overflow (_IO_FILE *fp, int c) +{ + if (c == EOF) + { + /* Updates the returned size location on stream flush. */ + struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp; + *mp->bufloc = fp->_wide_data->_IO_write_base; + *mp->sizeloc = (fp->_wide_data->_IO_write_ptr + - fp->_wide_data->_IO_write_base); + return 0; + } + return _IO_wstr_overflow (fp, c); +}