From patchwork Mon Oct 1 19:43:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 147930 Delivered-To: patch@linaro.org Received: by 2002:a2e:8595:0:0:0:0:0 with SMTP id b21-v6csp4302838lji; Mon, 1 Oct 2018 12:43:43 -0700 (PDT) X-Google-Smtp-Source: ACcGV62pNDuufr6RGHGP9v7Opt85wT/PEqPMoIY5NMTWrFhXmFQKMAz6aNXsBkVF8l4bg9wlTDbC X-Received: by 2002:a62:63c2:: with SMTP id x185-v6mr12833995pfb.13.1538423022951; Mon, 01 Oct 2018 12:43:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538423022; cv=none; d=google.com; s=arc-20160816; b=IyAQoYte3FBpKOK9RU0x77bR7DSD1vGIOT6+P1tEIB5CPRL28/wSaCTwwlADEitLh/ +aaGWtCEGT1HL/6r+td/42gQw0yF5+AwD1I7lPAgc7t2FoXK6wXOSYnK6HzzzQL8NY6D TQ7x8yofXRE7l3z0XOHcHPgY9pV8jZud63+nAle8it106UGZqqrdfGVvcj+4WPFOMaWg 5UvSfoj8jzP1V9dcY96tnl6jsOXEc0DpKT0w/gJbqjTuAjY0nUxpJGGdTINcR/ECtfo9 HpL7WE2qGrmgJoE3PzQGhOxvNUKfB9Pw5yjyK+OZcxsIYHLP49gi2V5uCAoaw8Sf+QnX emNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:to:from:dkim-signature:delivered-to:sender :list-help:list-post:list-archive:list-subscribe:list-unsubscribe :list-id:precedence:mailing-list:dkim-signature:domainkey-signature; bh=2PGRt+gKFLl+uuL5HhF0wbSFFG8P8Yvb5+ugpd6TVPU=; b=X/PuMdBM2/w2W/vrg1fZqTaNMebTCnxCOqGnH/H8eGMiIs0EUmXJlflGSG1qSB+ajp +fZowc6QdDLVee1ClyOr0UY9o5QGCFuy1f/+CUfii731AdRi3XJ0QXB9UolNCxUCbL4p ag7t6jUS7DgtyDcMLAc5iYS8BDWWQVG1W3z5iKYCyDSvF/xFGJCC1ZpDPcILoW+xm5iT K3u9yPl8053z7dbjEP/9s0FZABDbgW0PcsEtrq+gzmUkHZpqtQs6m6XLjXl/kStypg8a r6jIAK7O0hm4NEg8EoUSx9ZLr+zkBmCwd7Mg3Qksg73WNx8V4s8D4pcPBOpCMCyS6bTU XWBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=s+bJc+kY; dkim=pass header.i=@linaro.org header.s=google header.b="PhE/1SCN"; spf=pass (google.com: domain of libc-alpha-return-96199-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-96199-patch=linaro.org@sourceware.org"; dmarc=pass (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 r12-v6si12567529pgg.209.2018.10.01.12.43.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 01 Oct 2018 12:43:42 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-96199-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=s+bJc+kY; dkim=pass header.i=@linaro.org header.s=google header.b="PhE/1SCN"; spf=pass (google.com: domain of libc-alpha-return-96199-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-96199-patch=linaro.org@sourceware.org"; dmarc=pass (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=XqQLCWGXhJIOMvBf01MfWhU8bD3D3cVx5lpIKe/7d4efhfkMKMQ4i 4SZzPiZv8Mx0PP/Ct/mcx7p2UfiNSFEkPduzssxe2/Z3hkP1LQs70T2rczTO1N2V 1PlKUWieh3f8YXb/9rHOM/Q7tVlP4kj0fFwaUCjBOJA7DN0Su0IFvw= 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=vs2BsXR8k64NUTN7JZK+gX6EsXk=; b=s+bJc+kYmb0k0sAwfY0EFRSW+KYx FDHAPvsioKv+dDD1DQKdiW6+fE6MAWR06T13uP7+AiVNOrPpQ1ODCYhFz+I3QDvh okUe+6cntXlOHB/JqV922J4bvFe2V+dZAwALt4/FBXvti67Vxj3/ayZphxDgtzUA 4T1fVElEcoulMXs= Received: (qmail 61729 invoked by alias); 1 Oct 2018 19:43:33 -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 61716 invoked by uid 89); 1 Oct 2018 19:43:32 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=holders, mounted, 1019, UD:stat.h X-HELO: mail-qk1-f179.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id; bh=2PGRt+gKFLl+uuL5HhF0wbSFFG8P8Yvb5+ugpd6TVPU=; b=PhE/1SCNTXBMUW4ZKGWQ6zp9Wti4YpqWdzktuXTN2bGy5azFZ/8LcUniLdt6Vkht1O UPiLhbldF+ad5EO24xxOq3HKooqgZOhJ2UtcHqTTnK1nG2aCgF8R2pEREKdnFeDY23bY 4IRCgiZ58SN4jkdpp7B7Z5xevgnysNHWcrmVI= Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v3] libio: Flush stream at freopen (BZ#21037) Date: Mon, 1 Oct 2018 16:43:22 -0300 Message-Id: <20181001194322.4104-1-adhemerval.zanella@linaro.org> This is on my backlog for some time and based on previous review by Paul Eggert [1] I will commit it shortly if no one opposes it. Changes from previous version are just to use better libsupport check macros and to use an unsigned parameter on fd_to_filename to avoid require a larger value for FD_TO_FILENAME_SIZE (since _fitoa_word VALUE argument is usually an unsigned long int). -- As POSIX states [2] a freopen call should first flush the stream as if by a call fflush. C99 (n1256) and C11 (n1570) only states the function should first close any file associated with the specific stream. Although current implementation only follow C specification, current BSD and other libc implementation (musl) are in sync with POSIX and fflush the stream. This patch change freopen{64} to fflush the stream before actually reopening it (or returning if the stream does not support reopen). It also changes the Linux implementation to avoid a dynamic allocation on 'fd_to_filename'. Checked on x86_64-linux-gnu. [BZ #21037] * libio/Makefile (tests): Add tst-memstream4 and tst-wmemstream4. * libio/freopen.c (freopen): Sync stream before reopen and adjust to new fd_to_filename interface. * libio/freopen64.c (freopen64): Likewise. * libio/tst-memstream.h: New file. * libio/tst-memstream4.c: Likewise. * libio/tst-wmemstream4.c: Likewise. * sysdeps/generic/fd_to_filename.h (fd_to_filename): Change signature. * sysdeps/unix/sysv/linux/fd_to_filename.h (fd_to_filename): Likewise and remove internal dynamic allocation. [1] https://sourceware.org/ml/libc-alpha/2018-06/msg00424.html [2] http://pubs.opengroup.org/onlinepubs/9699919799/ --- ChangeLog | 14 +++++ libio/Makefile | 4 +- libio/freopen.c | 29 +++++----- libio/freopen64.c | 26 +++++---- libio/tst-memstream.h | 68 ++++++++++++++++++++++++ libio/tst-memstream4.c | 63 ++++++++++++++++++++++ libio/tst-wmemstream4.c | 20 +++++++ sysdeps/generic/fd_to_filename.h | 4 +- sysdeps/unix/sysv/linux/fd_to_filename.h | 34 +++++------- 9 files changed, 213 insertions(+), 49 deletions(-) create mode 100644 libio/tst-memstream.h create mode 100644 libio/tst-memstream4.c create mode 100644 libio/tst-wmemstream4.c -- 2.17.1 diff --git a/libio/Makefile b/libio/Makefile index cab0eae946..8239fba828 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -59,8 +59,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/freopen.c b/libio/freopen.c index abf19e96f1..6ce74ae449 100644 --- a/libio/freopen.c +++ b/libio/freopen.c @@ -24,28 +24,34 @@ This exception applies to code released by its copyright holders in files containing the exception. */ -#include "libioP.h" -#include "stdio.h" +#include #include #include #include -#include +#include #include - -#include +#include FILE * freopen (const char *filename, const char *mode, FILE *fp) { - FILE *result; + FILE *result = NULL; + char fdfilename[FD_TO_FILENAME_SIZE]; + CHECK_FILE (fp, NULL); - if (!(fp->_flags & _IO_IS_FILEBUF)) - return NULL; + _IO_acquire_lock (fp); + /* First flush the stream (failure should be ignored). */ + _IO_SYNC (fp); + + if (!(fp->_flags & _IO_IS_FILEBUF)) + goto end; + int fd = _IO_fileno (fp); - const char *gfilename = (filename == NULL && fd >= 0 - ? fd_to_filename (fd) : filename); + const char *gfilename + = filename != NULL ? filename : fd_to_filename (fd, fdfilename); + fp->_flags2 |= _IO_FLAGS2_NOCLOSE; #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1) if (&_IO_stdin_used == NULL) @@ -101,9 +107,6 @@ freopen (const char *filename, const char *mode, FILE *fp) __close (fd); end: - if (filename == NULL) - free ((char *) gfilename); - _IO_release_lock (fp); return result; } diff --git a/libio/freopen64.c b/libio/freopen64.c index ea7ebd3da4..055b999e59 100644 --- a/libio/freopen64.c +++ b/libio/freopen64.c @@ -24,27 +24,33 @@ This exception applies to code released by its copyright holders in files containing the exception. */ -#include "libioP.h" -#include "stdio.h" +#include #include #include #include +#include #include -#include - FILE * freopen64 (const char *filename, const char *mode, FILE *fp) { - FILE *result; + FILE *result = NULL; + char fdfilename[FD_TO_FILENAME_SIZE]; + CHECK_FILE (fp, NULL); - if (!(fp->_flags & _IO_IS_FILEBUF)) - return NULL; + _IO_acquire_lock (fp); + /* First flush the stream (failure should be ignored). */ + _IO_SYNC (fp); + + if (!(fp->_flags & _IO_IS_FILEBUF)) + goto end; + int fd = _IO_fileno (fp); - const char *gfilename = (filename == NULL && fd >= 0 - ? fd_to_filename (fd) : filename); + const char *gfilename + = filename != NULL ? filename : fd_to_filename (fd, fdfilename); + fp->_flags2 |= _IO_FLAGS2_NOCLOSE; _IO_file_close_it (fp); _IO_JUMPS_FILE_plus (fp) = &_IO_file_jumps; @@ -84,8 +90,6 @@ freopen64 (const char *filename, const char *mode, FILE *fp) __close (fd); end: - if (filename == NULL) - free ((char *) gfilename); _IO_release_lock (fp); return result; } diff --git a/libio/tst-memstream.h b/libio/tst-memstream.h new file mode 100644 index 0000000000..b9b02bdb17 --- /dev/null +++ b/libio/tst-memstream.h @@ -0,0 +1,68 @@ +/* Common definitions for open_memstream tests. + Copyright (C) 2018 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 FPUTS fputws +# 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 FPUTS fputs +# 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 0000000000..bb2635e837 --- /dev/null +++ b/libio/tst-memstream4.c @@ -0,0 +1,63 @@ +/* Test for open_memstream BZ #21037. + Copyright (C) 2018 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); + + /* Check if freopen proper fflush the stream. */ + { + CHAR_T old[] = W("old"); + CHAR_T *buf = old; + size_t size; + + FILE *fp = OPEN_MEMSTREAM (&buf, &size); + TEST_VERIFY_EXIT (fp != NULL); + + FPUTS (W("new"), fp); + /* The stream buffer pointer should be updated with only a fflush or + fclose. */ + TEST_COMPARE (STRCMP (buf, old), 0); + + /* The old stream should be fflush the stream, even for an invalid + streams. */ + FILE *nfp = freopen ("invalid-file", "r", fp); + TEST_VERIFY_EXIT (nfp == NULL); + + //TEST_VERIFY (STRCMP (buf, W("new")) == 0); + TEST_COMPARE_BLOB (buf, STRLEN (buf), W("new"), STRLEN (W ("new"))); + + TEST_COMPARE (fclose (fp), 0); + + free (buf); + } + + return 0; +} + +#include diff --git a/libio/tst-wmemstream4.c b/libio/tst-wmemstream4.c new file mode 100644 index 0000000000..8ff146e2d9 --- /dev/null +++ b/libio/tst-wmemstream4.c @@ -0,0 +1,20 @@ +/* Test for open_wmemstream BZ #21037. + Copyright (C) 2018 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/sysdeps/generic/fd_to_filename.h b/sysdeps/generic/fd_to_filename.h index bacfe5bf52..d41b345a9d 100644 --- a/sysdeps/generic/fd_to_filename.h +++ b/sysdeps/generic/fd_to_filename.h @@ -16,10 +16,12 @@ License along with the GNU C Library; if not, see . */ +#define FD_TO_FILENAME_SIZE 0 + /* In general there is no generic way to query filename for an open file descriptor. */ static inline const char * -fd_to_filename (int fd) +fd_to_filename (int fd, char *buf) { return NULL; } diff --git a/sysdeps/unix/sysv/linux/fd_to_filename.h b/sysdeps/unix/sysv/linux/fd_to_filename.h index 297716b0ae..62bc9916b0 100644 --- a/sysdeps/unix/sysv/linux/fd_to_filename.h +++ b/sysdeps/unix/sysv/linux/fd_to_filename.h @@ -16,30 +16,20 @@ License along with the GNU C Library; if not, see . */ -#include -#include #include -#include <_itoa.h> + +#define FD_TO_FILENAME_SIZE ((sizeof ("/proc/self/fd/") - 1) \ + + (sizeof ("4294967295") - 1) + 1) static inline const char * -fd_to_filename (int fd) +fd_to_filename (unsigned int fd, char *buf) { - char *ret = malloc (30); - - if (ret != NULL) - { - struct stat64 st; - - *_fitoa_word (fd, __stpcpy (ret, "/proc/self/fd/"), 10, 0) = '\0'; - - /* We must make sure the file exists. */ - if (__lxstat64 (_STAT_VER, ret, &st) < 0) - { - /* /proc is not mounted or something else happened. Don't - return the file name. */ - free (ret); - ret = NULL; - } - } - return ret; + *_fitoa_word (fd, __stpcpy (buf, "/proc/self/fd/"), 10, 0) = '\0'; + + /* We must make sure the file exists. */ + struct stat64 st; + if (__lxstat64 (_STAT_VER, buf, &st) < 0) + /* /proc is not mounted or something else happened. */ + return NULL; + return buf; }