public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: DJ Delorie <dj@redhat.com>
To: Andreas Schwab <schwab@suse.de>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH] Discard any push-back from a memory stream before finishing it (bug 34020)
Date: Mon, 30 Mar 2026 15:07:52 -0400	[thread overview]
Message-ID: <xnse9h87av.fsf@greed.delorie.com> (raw)
In-Reply-To: <mvm7bqtiivh.fsf@suse.de> (message from Andreas Schwab on Mon, 30 Mar 2026 14:47:46 +0200)

Andreas Schwab <schwab@suse.de> writes:
> This makes sure that the write base is pointing to the main area, not the
> backup area.

So I just realized, after working in this code for years, that "backup"
here doesn't mean "in case we run out" it means "going backwards" :-P No
wonder it seemed weird to me.

One question below about fseek, but otherwise OK.

> 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)

Ok.

> diff --git a/libio/tst-memstream6.c b/libio/tst-memstream6.c
> +static void
> +mcheck_abort (enum mcheck_status ev)
> +{
> +  printf ("mecheck failed with status %d\n", (int) ev);
> +  exit (1);
> +}

Ok.

> +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'));

I would have expected you to need an fseek here.  fflush() on a
memstream synchronizes the buffer and size pointers but says nothing
about moving the file pointer.

> +  TEST_COMPARE (UNGETC (W('B'), fp), W('B'));
> +
> +  TEST_COMPARE (fclose (fp), 0);
> +
> +  TEST_COMPARE (buf[0], W('A'));
> +
> +  free (buf);
> +}

Ok.

> +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);
> +}

Ok.

> +static int
> +do_test (void)
> +{
> +  mcheck_pedantic (mcheck_abort);
> +
> +  do_test_ungetc_1 ();
> +  do_test_ungetc_2 ();
> +
> +  return 0;
> +}

Ok.

> diff --git a/libio/tst-wmemstream6.c b/libio/tst-wmemstream6.c
> +#define TEST_WCHAR
> +#include <libio/tst-memstream6.c>

Ok.

> 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)

Ok.


  reply	other threads:[~2026-03-30 19:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-30 12:47 Andreas Schwab
2026-03-30 19:07 ` DJ Delorie [this message]
2026-04-02 19:12   ` Adhemerval Zanella Netto
2026-04-07 10:26     ` Andreas Schwab
2026-04-07 16:14       ` Adhemerval Zanella Netto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xnse9h87av.fsf@greed.delorie.com \
    --to=dj@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=schwab@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).