From patchwork Thu Jul 16 11:46:43 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 7717 X-Patchwork-Delegate: azanella@linux.vnet.ibm.com Received: (qmail 26972 invoked by alias); 16 Jul 2015 11:46:51 -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 26948 invoked by uid 89); 16 Jul 2015 11:46:50 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-yk0-f180.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :cc:subject:content-type:content-transfer-encoding; bh=Es0GoeK5/F/n+OSENG1FJeokZWmCm5C/hann4yMzUcY=; b=NBWHfUPzjNNKFNQSuJs220cetGX2kNggnQ66LLG5vAH8ji0B2DdxDp/muSsrOELSFy 2+LFmXCLpYqjgmib8GDkiguW9Y81txt2n9LM+Hlge67aXesckWw3aBlhuvN1umoR9tqj LXP1WQl+AwKMnjugPnVLocix0obniI2BTEBZQ4aNskzgt8j5o9RDZbYQrgsW2mgk10Sa oEcKwXnA+s16eYTQP+pjy/mDiabndGikxdR1HZV56hqri8EA4sGQMuNx4lO4Fwguvq0u ZUknUWZyLVeXOVl7bRMaBC6IvPGxE9eDPm8rU3fipIWWcuRKiLZUEWeRgz08BBhvy3ar ZE1g== X-Gm-Message-State: ALoCoQmSLwzKXzgni/ufABoXHayY7eJMNcGYRjwvZzL7Crd3vjk9ag7xzFxbjfCEkZT5S3Pv4AmZ X-Received: by 10.13.240.67 with SMTP id z64mr9263857ywe.69.1437047206794; Thu, 16 Jul 2015 04:46:46 -0700 (PDT) Message-ID: <55A799A3.6040202@linaro.org> Date: Thu, 16 Jul 2015 08:46:43 -0300 From: Adhemerval Zanella User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: GNU C Library CC: Carlos O'Donell Subject: [PATCH] libio: Fix fmemopen 'w' mode with provided buffer If 'w' mode is used with a provided buffer the fmemopen will try to find the first null byte to set as maximum internal stream size. It should be done only for append mode ('a'). Kudos for Stefan Liebler for finding this error on s390-32. --- * libio/fmemopen.c (__fmemopen): Fix 'w' openmode with provided buffer. * stdio-common/tst-fmemopen2.c (do_test_with_buffer): Typo. -- diff --git a/libio/fmemopen.c b/libio/fmemopen.c index e6e6a49..3ab3e8d 100644 --- a/libio/fmemopen.c +++ b/libio/fmemopen.c @@ -150,7 +150,7 @@ __fmemopen (void *buf, size_t len, const char *mode) cookie_io_functions_t iof; fmemopen_cookie_t *c; - c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t)); + c = (fmemopen_cookie_t *) calloc (sizeof (fmemopen_cookie_t), 1); if (c == NULL) return NULL; @@ -165,7 +165,6 @@ __fmemopen (void *buf, size_t len, const char *mode) return NULL; } c->buffer[0] = '\0'; - c->maxpos = 0; } else { @@ -182,7 +181,8 @@ __fmemopen (void *buf, size_t len, const char *mode) if (mode[0] == 'w' && mode[1] == '+') c->buffer[0] = '\0'; - c->maxpos = strnlen (c->buffer, len); + if (mode[0] == 'a') + c->maxpos = strnlen (c->buffer, len); } diff --git a/stdio-common/tst-fmemopen2.c b/stdio-common/tst-fmemopen2.c index 16dd3ad..ecd7824 100644 --- a/stdio-common/tst-fmemopen2.c +++ b/stdio-common/tst-fmemopen2.c @@ -34,7 +34,7 @@ do_test_with_buffer (void) FILE *fp = fmemopen (buf, nbuf, "w"); if (fp == NULL) { - printf ("FAIL: fmemopen failedi (%s)\n", __FUNCTION__); + printf ("FAIL: fmemopen failed (%s)\n", __FUNCTION__); return 1; }