From patchwork Mon Mar 30 12:47:46 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Schwab X-Patchwork-Id: 132451 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 4F8874BA2E3E for ; Mon, 30 Mar 2026 12:48:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4F8874BA2E3E Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=Smj8zkmi; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=jXhHYwaZ; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=Smj8zkmi; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=jXhHYwaZ X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by sourceware.org (Postfix) with ESMTPS id 6C0494BA9001 for ; Mon, 30 Mar 2026 12:47:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6C0494BA9001 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6C0494BA9001 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1774874868; cv=none; b=mdg5cX8r4Cbf2cW/xbuUIjIdUp2cLNe5QRgSBuG1uMOVuIbcheDH2S64h1gpm4XVS5VjgwdgGDxQmmMWCBk3ney269UIp+tfODOA1bvrwpwIPp9u9Ywe/2ABRCih5RdBockaKAkFql1nJ64t4vjqNaHYA7vdnudGl65NUYpzfog= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1774874868; c=relaxed/simple; bh=k3rSJuO+VQbK/kWE4WyDAPWevcyTmUYUFkUB0Ujjjf8=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-ID:MIME-Version; b=Sw0E62Xu4hMuw4VuCD4Zp5vvXYnP4wNTbR93bz+bpdnWsNDmooYuH/tbTvgz58XLKLuwbnt170MGSg4od8Yetxdcw3oaxdUYqWCWp0fQIiWjuAT4XFHzmPlfvOdA4Vl91DtmbnF2sbLuJ6NtugRfoM/s9+ibVdICPY2mDGW1ocE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6C0494BA9001 Received: from hawking.suse.de (unknown [IPv6:2a07:de40:a101:3:92b1:1cff:fe69:ddc]) by smtp-out1.suse.de (Postfix) with ESMTP id 432E94D5A7 for ; Mon, 30 Mar 2026 12:47:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1774874867; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=u6TJHiS+Pk1v1dTdJdlwyXLcP0PHIVHFN8oV5O3D2Ew=; b=Smj8zkmikXwFMM5/LrcK40KqawVON5Ki4QdI9shQWPz5mh6Zoo0gJVwFOFekRSWykz+6sC CQ386o4O7a7U7gEv4lVnEU+t8bE7d6i35U8dVLCEpV8b1lPlp4dXoHSAMFee3bW7ObCBAe 1NdM0b8ukZ9ZCOxFkjpcIjNtH8GaegE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1774874867; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=u6TJHiS+Pk1v1dTdJdlwyXLcP0PHIVHFN8oV5O3D2Ew=; b=jXhHYwaZujZEiz/T9wPdi7J3c3B1W3M3i4uq7OeZIwHx9+cXNS2riaqKGzcuDeJscSMtJY xupNx75pV8CzzjAQ== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=Smj8zkmi; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=jXhHYwaZ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1774874867; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=u6TJHiS+Pk1v1dTdJdlwyXLcP0PHIVHFN8oV5O3D2Ew=; b=Smj8zkmikXwFMM5/LrcK40KqawVON5Ki4QdI9shQWPz5mh6Zoo0gJVwFOFekRSWykz+6sC CQ386o4O7a7U7gEv4lVnEU+t8bE7d6i35U8dVLCEpV8b1lPlp4dXoHSAMFee3bW7ObCBAe 1NdM0b8ukZ9ZCOxFkjpcIjNtH8GaegE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1774874867; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=u6TJHiS+Pk1v1dTdJdlwyXLcP0PHIVHFN8oV5O3D2Ew=; b=jXhHYwaZujZEiz/T9wPdi7J3c3B1W3M3i4uq7OeZIwHx9+cXNS2riaqKGzcuDeJscSMtJY xupNx75pV8CzzjAQ== Received: by hawking.suse.de (Postfix, from userid 17005) id F06644A04D9; Mon, 30 Mar 2026 14:47:46 +0200 (CEST) From: Andreas Schwab To: libc-alpha@sourceware.org Subject: [PATCH] Discard any push-back from a memory stream before finishing it (bug 34020) Date: Mon, 30 Mar 2026 14:47:46 +0200 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Spamd-Bar: +++++++++++++++ X-Rspamd-Queue-Id: 432E94D5A7 X-Rspamd-Action: add header X-Spam-Score: 15.28 X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spamd-Result: default: False [15.28 / 50.00]; SPAM_FLAG(5.00)[]; NEURAL_SPAM_LONG(3.50)[1.000]; BAYES_HAM(-3.00)[100.00%]; NEURAL_SPAM_SHORT(2.99)[0.995]; HFILTER_HOSTNAME_UNKNOWN(2.50)[]; RDNS_NONE(2.00)[]; ONCE_RECEIVED(1.20)[]; HFILTER_HELO_IP_A(1.00)[hawking.suse.de]; HFILTER_HELO_NORES_A_OR_MX(0.30)[hawking.suse.de]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MIME_GOOD(-0.10)[text/plain]; RCVD_NO_TLS_LAST(0.10)[]; MX_GOOD(-0.01)[]; FROM_HAS_DN(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; DIRECT_TO_MX(0.00)[Gnus/5.13 (Gnus v5.13)]; FUZZY_RATELIMITED(0.00)[rspamd.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_COUNT_ONE(0.00)[1]; MID_RHS_MATCH_FROM(0.00)[]; DNSWL_BLOCKED(0.00)[2a07:de40:a101:3:92b1:1cff:fe69:ddc:from]; DKIM_TRACE(0.00)[suse.de:+]; MIME_TRACE(0.00)[0:+]; TO_DN_NONE(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:mid, suse.de:dkim, suse.de:email, sourceware.org:url] X-Spam: Yes X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org This makes sure that the write base is pointing to the main area, not the backup area. --- The wide char test requires https://patchwork.sourceware.org/project/glibc/patch/20260323171742.1039768-1-marocketbd@gmail.com/ to pass. --- libio/Makefile | 2 + libio/memstream.c | 3 ++ libio/tst-memstream.h | 4 ++ libio/tst-memstream6.c | 81 +++++++++++++++++++++++++++++++++++++++++ libio/tst-wmemstream6.c | 20 ++++++++++ libio/wmemstream.c | 3 ++ 6 files changed, 113 insertions(+) create mode 100644 libio/tst-memstream6.c create mode 100644 libio/tst-wmemstream6.c diff --git a/libio/Makefile b/libio/Makefile index da838cdecc..4582291b24 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -121,6 +121,7 @@ tests = \ tst-memstream2 \ tst-memstream3 \ tst-memstream4 \ + tst-memstream6 \ tst-mmap-eofsync \ tst-mmap-fflushsync \ tst-mmap-offend \ @@ -144,6 +145,7 @@ tests = \ tst-wmemstream3 \ tst-wmemstream4 \ tst-wmemstream5 \ + tst-wmemstream6 \ tst_getwc \ tst_putwc \ tst_swprintf \ diff --git a/libio/memstream.c b/libio/memstream.c index 0456adb92f..fcc925df87 100644 --- a/libio/memstream.c +++ b/libio/memstream.c @@ -100,6 +100,9 @@ _IO_mem_finish (FILE *fp, int dummy) { struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp; + if (_IO_in_backup (fp)) + _IO_switch_to_main_get_area (fp); + *mp->bufloc = (char *) realloc (fp->_IO_write_base, fp->_IO_write_ptr - fp->_IO_write_base + 1); if (*mp->bufloc != NULL) diff --git a/libio/tst-memstream.h b/libio/tst-memstream.h index b420e32e66..581cfd2cc3 100644 --- a/libio/tst-memstream.h +++ b/libio/tst-memstream.h @@ -50,6 +50,8 @@ fwwrite (const void *ptr, size_t size, size_t nmemb, FILE *arq) # define FWRITE fwwrite # define FPUTC fputwc # define FPUTS fputws +# define FGETC fgetwc +# define UNGETC ungetwc # define STRCMP wcscmp # define STRLEN wcslen #else @@ -60,6 +62,8 @@ fwwrite (const void *ptr, size_t size, size_t nmemb, FILE *arq) # define FWRITE fwrite # define FPUTC fputc # define FPUTS fputs +# define FGETC fgetc +# define UNGETC ungetc # define STRCMP strcmp # define STRLEN strlen #endif diff --git a/libio/tst-memstream6.c b/libio/tst-memstream6.c new file mode 100644 index 0000000000..128e733b98 --- /dev/null +++ b/libio/tst-memstream6.c @@ -0,0 +1,81 @@ +/* Test for open_memstream BZ #34020. + Copyright (C) 2018-2026 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); +} + + +void +do_test_ungetc_1 (void) +{ + CHAR_T *buf; + size_t size; + + FILE *fp = OPEN_MEMSTREAM (&buf, &size); + TEST_VERIFY_EXIT (fp != NULL); + + FPUTC (W('A'), fp); + fflush (fp); + + TEST_COMPARE (FGETC (fp), W('A')); + TEST_COMPARE (UNGETC (W('B'), fp), W('B')); + + TEST_COMPARE (fclose (fp), 0); + + TEST_COMPARE (buf[0], W('A')); + + free (buf); +} + + +void +do_test_ungetc_2 (void) +{ + CHAR_T *buf; + size_t size; + + FILE *fp = OPEN_MEMSTREAM (&buf, &size); + TEST_VERIFY_EXIT (fp != NULL); + + TEST_COMPARE (UNGETC (W('A'), fp), W('A')); + + TEST_COMPARE (fclose (fp), 0); + + TEST_COMPARE (buf[0], 0); + + free (buf); +} + +static int +do_test (void) +{ + mcheck_pedantic (mcheck_abort); + + do_test_ungetc_1 (); + do_test_ungetc_2 (); + + return 0; +} + +#include diff --git a/libio/tst-wmemstream6.c b/libio/tst-wmemstream6.c new file mode 100644 index 0000000000..23c50197cf --- /dev/null +++ b/libio/tst-wmemstream6.c @@ -0,0 +1,20 @@ +/* Test for open_wmemstream BZ #34020. + Copyright (C) 2026 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 d0c639be70..a0c61a627f 100644 --- a/libio/wmemstream.c +++ b/libio/wmemstream.c @@ -101,6 +101,9 @@ _IO_wmem_finish (FILE *fp, int dummy) { struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp; + if (_IO_in_backup (fp)) + _IO_switch_to_main_wget_area (fp); + *mp->bufloc = (wchar_t *) realloc (fp->_wide_data->_IO_write_base, (fp->_wide_data->_IO_write_ptr - fp->_wide_data->_IO_write_base + 1)