[PATCH v3] libio: Fix fmemopen_write on appending condition

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Wed May 13 11:39:34 GMT 2026



On 24/03/26 14:22, Rocket Ma wrote:
> * libio/fmemopen.c: reference pos the variable instead of c->pos
> 
> To reproduce, write a C unit with the following code and then compile it
> and run it, should expect an output with "1 No space left on device".
> Apparently, for appending mode, there is enough room to write some
> bytes, but it didn't. `c->pos` should be corrected to `pos`.
> 
> Signed-off-by: Rocket Ma <marocketbd@gmail.com>

I think the patch is correct in the end, some comments below. 

Add the bug 34006 on the title (BZ 34006).

> ---
>  libio/Makefile       |  1 +
>  libio/bug-fmemopen.c | 37 +++++++++++++++++++++++++++++++++++++
>  libio/fmemopen.c     |  2 +-
>  3 files changed, 39 insertions(+), 1 deletion(-)
>  create mode 100644 libio/bug-fmemopen.c
> 
> diff --git a/libio/Makefile b/libio/Makefile
> index 08e1e0ec25..c60ebf800d 100644
> --- a/libio/Makefile
> +++ b/libio/Makefile
> @@ -70,6 +70,7 @@ routines_no_fortify += \
>    # routines_no_fortify
>  
>  tests = \
> +  bug-fmemopen \
>    bug-fopena+ \
>    bug-fseek \
>    bug-ftell \
> diff --git a/libio/bug-fmemopen.c b/libio/bug-fmemopen.c
> new file mode 100644
> index 0000000000..080ea5835c
> --- /dev/null
> +++ b/libio/bug-fmemopen.c
> @@ -0,0 +1,37 @@
> +/* Regression test for fmemopen bug BZ 34006
> +   Copyright (C) 2021-2023 Free Software Foundation, Inc.

s/2021-2023/2026.

> +   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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/check.h>
> +#include <stdio.h>
> +
> +static int
> +do_test (void)
> +{
> +  char buf[5] = "1";
> +  FILE *fp = fmemopen (buf, 4, "a+");
> +  TEST_VERIFY (fp);
> +  TEST_VERIFY (fseek (fp, 3, SEEK_SET) == 0);
> +  setbuf (fp, NULL);

I think the standard states setbuf/setvbuf to be called between fopen and 
any other I/O on the stream.

> +  TEST_VERIFY (fwrite ("XXXX", 1, 4, fp) > 0);
> +  TEST_COMPARE_STRING (buf, "1XXX");
> +  fclose (fp);
> +
> +  return 0;
> +}
> +
> +#include <support/test-driver.c>
> diff --git a/libio/fmemopen.c b/libio/fmemopen.c
> index f2ae1338d3..cdc3a3476e 100644
> --- a/libio/fmemopen.c
> +++ b/libio/fmemopen.c
> @@ -71,7 +71,7 @@ fmemopen_write (void *cookie, const char *b, size_t s)
>  
>    if (pos + s > c->size)
>      {
> -      if ((size_t) (c->pos + addnullc) >= c->size)
> +      if ((size_t) (pos + addnullc) >= c->size)
>  	{
>  	  __set_errno (ENOSPC);
>  	  return 0;



More information about the Libc-alpha mailing list