[v3] libio: Fix fmemopen_write on appending condition

Message ID 20260324172242.16403-1-marocketbd@gmail.com (mailing list archive)
State New
Headers
Series [v3] libio: Fix fmemopen_write on appending condition |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed

Commit Message

Rocket Ma March 24, 2026, 5:22 p.m. UTC
  * 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>
---
 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
  

Patch

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