5583ed7
Author: Florian Weimer <fweimer@redhat.com>
5583ed7
Date:   Tue Jan 31 09:44:16 2023 +0100
5583ed7
5583ed7
    libio: Update number of written bytes in dprintf implementation
5583ed7
    
5583ed7
    The __printf_buffer_flush_dprintf function needs to record that
5583ed7
    the buffer has been written before reusing it.  Without this
5583ed7
    accounting, dprintf always returns zero.
5583ed7
    
5583ed7
    Fixes commit 8ece45e4f586abd212d1c02d74d38ef681a45600
5583ed7
    ("libio: Convert __vdprintf_internal to buffers").
5583ed7
5583ed7
diff --git a/libio/iovdprintf.c b/libio/iovdprintf.c
5583ed7
index fb359d263de3428c..d9fa886fdf3e2f53 100644
5583ed7
--- a/libio/iovdprintf.c
5583ed7
+++ b/libio/iovdprintf.c
5583ed7
@@ -54,6 +54,7 @@ __printf_buffer_flush_dprintf (struct __printf_buffer_dprintf *buf)
5583ed7
 	}
5583ed7
       p += ret;
5583ed7
     }
5583ed7
+  buf->base.written += buf->base.write_ptr - buf->base.write_base;
5583ed7
   buf->base.write_ptr = buf->buf;
5583ed7
 }
5583ed7
 
5583ed7
diff --git a/stdio-common/Makefile b/stdio-common/Makefile
5583ed7
index da3034d847578074..34fdd6d1f890c282 100644
5583ed7
--- a/stdio-common/Makefile
5583ed7
+++ b/stdio-common/Makefile
5583ed7
@@ -180,6 +180,7 @@ tests := \
5583ed7
   tst-bz11319 \
5583ed7
   tst-bz11319-fortify2 \
5583ed7
   tst-cookie \
5583ed7
+  tst-dprintf-length \
5583ed7
   tst-fdopen \
5583ed7
   tst-ferror \
5583ed7
   tst-fgets \
5583ed7
diff --git a/stdio-common/tst-dprintf-length.c b/stdio-common/tst-dprintf-length.c
5583ed7
new file mode 100644
5583ed7
index 0000000000000000..abe2caf45aa46255
5583ed7
--- /dev/null
5583ed7
+++ b/stdio-common/tst-dprintf-length.c
5583ed7
@@ -0,0 +1,45 @@
5583ed7
+/* Test that dprintf returns the expected length.
5583ed7
+   Copyright (C) 2023 Free Software Foundation, Inc.
5583ed7
+   This file is part of the GNU C Library.
5583ed7
+
5583ed7
+   The GNU C Library is free software; you can redistribute it and/or
5583ed7
+   modify it under the terms of the GNU Lesser General Public
5583ed7
+   License as published by the Free Software Foundation; either
5583ed7
+   version 2.1 of the License, or (at your option) any later version.
5583ed7
+
5583ed7
+   The GNU C Library is distributed in the hope that it will be useful,
5583ed7
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
5583ed7
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5583ed7
+   Lesser General Public License for more details.
5583ed7
+
5583ed7
+   You should have received a copy of the GNU Lesser General Public
5583ed7
+   License along with the GNU C Library; if not, see
5583ed7
+   <https://www.gnu.org/licenses/>.  */
5583ed7
+
5583ed7
+#include <stdio.h>
5583ed7
+#include <string.h>
5583ed7
+#include <support/check.h>
5583ed7
+#include <sys/socket.h>
5583ed7
+#include <unistd.h>
5583ed7
+
5583ed7
+static int
5583ed7
+do_test (void)
5583ed7
+{
5583ed7
+  /* Use a datagram socket to check that everything arrives in one packet.
5583ed7
+     The dprintf function should perform a single write call.  */
5583ed7
+  int fds[2];
5583ed7
+  TEST_VERIFY_EXIT (socketpair (AF_LOCAL, SOCK_DGRAM, 0, fds) == 0);
5583ed7
+
5583ed7
+  TEST_COMPARE (dprintf (fds[0], "(%d)%s[%d]", 123, "---", 4567), 14);
5583ed7
+
5583ed7
+  char buf[32];
5583ed7
+  ssize_t ret = read (fds[1], buf, sizeof (buf));
5583ed7
+  TEST_VERIFY_EXIT (ret > 0);
5583ed7
+  TEST_COMPARE_BLOB (buf, ret, "(123)---[4567]", strlen ("(123)---[4567]"));
5583ed7
+
5583ed7
+  close (fds[1]);
5583ed7
+  close (fds[0]);
5583ed7
+  return 0;
5583ed7
+}
5583ed7
+
5583ed7
+#include <support/test-driver.c>