From patchwork Mon Jul 10 16:19:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 107313 Delivered-To: patch@linaro.org Received: by 10.140.101.44 with SMTP id t41csp3632251qge; Mon, 10 Jul 2017 09:20:07 -0700 (PDT) X-Received: by 10.84.232.7 with SMTP id h7mr18924369plk.193.1499703606624; Mon, 10 Jul 2017 09:20:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1499703606; cv=none; d=google.com; s=arc-20160816; b=s3QgxgLPjMEHL8AjSI3VqGLukS4ndspFHj+Ct4nhBLvJtYq6lNTHWxD7CsuqgH4GGp 6J+AO8u7cm/jOx3qVx44568SxFUv0pUSnUuJcpjOYCGIzxzMTH9hyVsP20z6V2REuHfS CEdV7mZpbhlZxCp59kVuEijPxCYnG3+pocCMWgUxA/93NBY5RCbIwybYJym9EC51me0x bydhUtl04nXDBvUbb3AbVNnmzkEgoLF1/z/ZaGKkXJjllqymNghzausz9tFvjo40WvPn NIrRZA3nn+oe8PrpXFxKQn2SOQ+822sg7Oh1r9CmATJ9AFDN8MLYEfOB8EKqKXPvWs7B t0zg== 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=uBSGJ5j2twLTVEVoOlmE12osZnQDqRCkhxUGWxBkYyw=; b=oP2bsXyd+KlshyBblmuaGbCknix+gX/xtWnembT6gKn3maEcGc5mPPzqu/4m3Gk1tm ee86mqcXEsnGq40OMlo6XA4Juf7DjDepHwF4AT34rd/x2yrjNpkhqvIhMiVJj5SwC47X K0cdlW7SNyMS1woNNbvrySyJsDDMdfGGXpAImM+N/q4fZZDMUAdsxCcXIOFSeAus7Qhl 5tdasE2x0eNzAXO/ekLKGDcj9CmIelWVZzN5cBOzBwI7wEQByOOeiYVEnbCcq051rxAf ++kLyzscqXBur9+kLWtv80/exPeg9RDWFU7YrCUEZbeTKSRxLGmDhVDJ6W2PW4onXqpZ eP4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.b=ghJiSq8k; spf=pass (google.com: domain of libc-alpha-return-81960-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-81960-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 t202si2300190pgb.7.2017.07.10.09.20.06 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 10 Jul 2017 09:20:06 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-81960-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.b=ghJiSq8k; spf=pass (google.com: domain of libc-alpha-return-81960-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-81960-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=UtRuiYGphox6H8hvrHNhJ+IaDPvfB8UUVvjQh6C4W+UoRmI8/t0KY SOTfqJwVQ9B+qPcICJUYQI4Kj+hdoc+0TAs927dkinuw/CxCc0lHZmggcGafHKHm P3Lt3qehR3uBRQY4Amke7Adb6J6XzCmwMsU5V6mTfP+zPvhMnfDgv4= 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=1mBzGgEnUJ4dFd1kEhuQvKx1BQs=; b=ghJiSq8kJZKaUIJg3r+7tRnJGNnQ 787eoJdTqVcdHW/fgNMvdXMKeWd3o8CWcg4QVYKvs9Sxg02nH20IFBswglWhCIEf NTN4NjdeWl8BQVPjHfYtMnsswcDg7URxDd4b5kyQS+FcFHjuXhlaIklG/dU/1IZP dkeC47JaG+LDSno= Received: (qmail 89746 invoked by alias); 10 Jul 2017 16:19:56 -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 89723 invoked by uid 89); 10 Jul 2017 16:19:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, RCVD_IN_SORBS_SPAM, SPF_PASS, UNWANTED_LANGUAGE_BODY autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f178.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=uBSGJ5j2twLTVEVoOlmE12osZnQDqRCkhxUGWxBkYyw=; b=LxKE7S8m8trJa2qmLruFIjphHxsfcYyLRG4pAW/nxcbmE2tgdJm/vhesQBfD1M/fQ2 ZjJEqrP8WUPG36X7UVwPTvd+UnP0BkPfn/oIdC0eelZcSW//8cyu5WUo56k9tsJzX/jz yCWpnfn6OG1DXlvEHpVRBO6Lypx1O4lL9j0Z5w/SeWzpY2SaPJVHbXxh3scO4puuDEAp HYTSklQHoP/8rre7zc8Tfk7EL2MZf8bZw1zvxh4t8lromy/YWqS2V4oOoJpMk5jL5Fz+ 7J732bkwKqMaKYYZtK9AJx+lNEOzHGYBMdlqq2HyQIFK4jdyRqrBV6FTnisTGM2w9yEk 4OZw== X-Gm-Message-State: AIVw111vlORw8QbxOHHy2A7ec/zeQwGBpSXlbb7mlOuulHkOJA3Jbvwm CYb2x2lISvDXjmuSx7XBjw== X-Received: by 10.237.39.97 with SMTP id n88mr5202841qtd.78.1499703590388; Mon, 10 Jul 2017 09:19:50 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH] libio: Fix open_memstream flush (NULL) Date: Mon, 10 Jul 2017 13:19:45 -0300 Message-Id: <1499703585-17933-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 and also adjust the tst-memstream3 to use libsupport. 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/tst-memstream3.c (mcheck_abort): Use libsupport. (do_test_bz18241): Likewise. (do_test_bz20181): Likewise. (do_test_bz21735): New function. --- ChangeLog | 14 +++++++ libio/memstream.c | 15 +++++++- libio/tst-memstream3.c | 99 +++++++++++++++++++++++++++++--------------------- libio/wmemstream.c | 16 +++++++- 4 files changed, 100 insertions(+), 44 deletions(-) -- 2.7.4 diff --git a/libio/memstream.c b/libio/memstream.c index f83d4a5..3be5b58 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; @@ -137,3 +139,14 @@ _IO_mem_finish (_IO_FILE *fp, int dummy) _IO_str_finish (fp, 0); } + +static int +_IO_mem_overflow (_IO_FILE *fp, int c) +{ + int ret = _IO_str_overflow (fp, c); + + struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp; + *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base; + + return ret; +} diff --git a/libio/tst-memstream3.c b/libio/tst-memstream3.c index ce201d1..395e5c2 100644 --- a/libio/tst-memstream3.c +++ b/libio/tst-memstream3.c @@ -23,6 +23,7 @@ #include #include +#include #ifndef CHAR_T # define CHAR_T char @@ -40,24 +41,9 @@ static void mcheck_abort (enum mcheck_status ev) { - printf ("mecheck failed with status %d\n", (int) ev); - exit (1); + FAIL_EXIT1 ("mecheck failed with status %d", (int) ev); } -static void -error_printf (int line, const char *fmt, ...) -{ - va_list ap; - - printf ("error: %s:%i: ", __FILE__, line); - va_start (ap, fmt); - vprintf (fmt, ap); - va_end (ap); -} - -#define ERROR_RET1(...) \ - { error_printf(__LINE__, __VA_ARGS__); return 1; } - static int do_test_bz18241 (void) { @@ -66,37 +52,37 @@ do_test_bz18241 (void) FILE *fp = OPEN_MEMSTREAM (&buf, &size); if (fp == NULL) - ERROR_RET1 ("%s failed\n", S(OPEN_MEMSTREAM)); + FAIL_RET ("%s failed", S(OPEN_MEMSTREAM)); if (FPUTC (W('a'), fp) != W('a')) - ERROR_RET1 ("%s failed (errno = %d)\n", S(FPUTC), errno); + FAIL_RET ("%s failed: %m", S(FPUTC)); if (fflush (fp) != 0) - ERROR_RET1 ("fflush failed (errno = %d)\n", errno); + FAIL_RET ("fflush failed: %m"); if (fseek (fp, -2, SEEK_SET) != -1) - ERROR_RET1 ("fseek failed (errno = %d)\n", errno); + FAIL_RET ("fseek failed: %m"); if (errno != EINVAL) - ERROR_RET1 ("errno != EINVAL\n"); + FAIL_RET ("errno != EINVAL"); if (ftell (fp) != 1) - ERROR_RET1 ("ftell failed (errno = %d)\n", errno); + FAIL_RET ("ftell failed: %m"); if (ferror (fp) != 0) - ERROR_RET1 ("ferror != 0\n"); + FAIL_RET ("ferror != 0"); if (fseek (fp, -1, SEEK_CUR) == -1) - ERROR_RET1 ("fseek failed (errno = %d)\n", errno); + FAIL_RET ("fseek failed: %m"); if (ftell (fp) != 0) - ERROR_RET1 ("ftell failed (errno = %d)\n", errno); + FAIL_RET ("ftell failed: %m"); if (ferror (fp) != 0) - ERROR_RET1 ("ferror != 0\n"); + FAIL_RET ("ferror != 0"); if (FPUTC (W('b'), fp) != W('b')) - ERROR_RET1 ("%s failed (errno = %d)\n", S(FPUTC), errno); + FAIL_RET ("%s failed: %m", S(FPUTC)); if (fflush (fp) != 0) - ERROR_RET1 ("fflush failed (errno = %d)\n", errno); + FAIL_RET ("fflush failed: %m"); if (fclose (fp) != 0) - ERROR_RET1 ("fclose failed (errno = %d\n", errno); + FAIL_RET ("fclose failed: %m"); if (STRCMP (buf, W("b")) != 0) - ERROR_RET1 ("%s failed\n", S(STRCMP)); + FAIL_RET ("%s failed", S(STRCMP)); free (buf); @@ -112,45 +98,74 @@ do_test_bz20181 (void) FILE *fp = OPEN_MEMSTREAM (&buf, &size); if (fp == NULL) - ERROR_RET1 ("%s failed\n", S(OPEN_MEMSTREAM)); + FAIL_RET ("%s failed\n", S(OPEN_MEMSTREAM)); if ((ret = FWRITE (W("abc"), 1, 3, fp)) != 3) - ERROR_RET1 ("%s failed (errno = %d)\n", S(FWRITE), errno); + FAIL_RET ("%s failed (errno = %d)\n", S(FWRITE), errno); if (fseek (fp, 0, SEEK_SET) != 0) - ERROR_RET1 ("fseek failed (errno = %d)\n", errno); + FAIL_RET ("fseek failed (errno = %d)\n", errno); if (FWRITE (W("z"), 1, 1, fp) != 1) - ERROR_RET1 ("%s failed (errno = %d)\n", S(FWRITE), errno); + FAIL_RET ("%s failed (errno = %d)\n", S(FWRITE), errno); if (fflush (fp) != 0) - ERROR_RET1 ("fflush failed (errno = %d)\n", errno); + FAIL_RET ("fflush failed (errno = %d)\n", errno); /* Avoid truncating the buffer on close. */ if (fseek (fp, 3, SEEK_SET) != 0) - ERROR_RET1 ("fseek failed (errno = %d)\n", errno); + FAIL_RET ("fseek failed (errno = %d)\n", errno); if (fclose (fp) != 0) - ERROR_RET1 ("fclose failed (errno = %d\n", errno); + FAIL_RET ("fclose failed (errno = %d\n", errno); if (size != 3) - ERROR_RET1 ("size != 3\n"); + FAIL_RET ("size != 3\n"); if (buf[0] != W('z') || buf[1] != W('b') || buf[2] != W('c')) { PRINTF (W("error: buf {%c,%c,%c} != {z,b,c}\n"), - buf[0], buf[1], buf[2]); - return 1; + buf[0], buf[1], buf[2]); + FAIL_RET (NULL); } + free (buf); return 0; } static int +do_test_bz21735 (void) +{ + CHAR_T *buf; + size_t size; + + FILE *fp = OPEN_MEMSTREAM (&buf, &size); + if (fp == NULL) + FAIL_RET ("%s failed", S(OPEN_MEMSTREAM)); + + if (FPUTC (W('a'), fp) != W('a')) + FAIL_RET ("%s failed: %m", S(FPUTC)); + + if (fflush (fp) != 0) + FAIL_RET ("fflush failed: %m"); + + if (size != 1) + FAIL_RET ("size != 1"); + + if (fileno (fp) != -1) + FAIL_RET ("fileno returned a valid integer descriptor"); + + if (fclose (fp) != 0) + FAIL_RET ("fclose: %m"); + + return 0; +} + +static int do_test (void) { int ret = 0; @@ -159,9 +174,9 @@ do_test (void) ret += do_test_bz18241 (); ret += do_test_bz20181 (); + ret += do_test_bz21735 (); return ret; } -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" +#include diff --git a/libio/wmemstream.c b/libio/wmemstream.c index 5bc77f5..f6d9f14 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; @@ -142,3 +144,15 @@ _IO_wmem_finish (_IO_FILE *fp, int dummy) _IO_wstr_finish (fp, 0); } + +static int +_IO_wmem_overflow (_IO_FILE *fp, int c) +{ + int ret = _IO_wstr_overflow (fp, c); + + struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp; + *mp->sizeloc = (fp->_wide_data->_IO_write_ptr + - fp->_wide_data->_IO_write_base); + + return ret; +}