55f6e7
commit 29b12753b51866b227a6c0ac96c2c6c0e20f3497
55f6e7
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
55f6e7
Date:   Thu Mar 19 18:35:46 2020 -0300
55f6e7
55f6e7
    stdio: Add tests for printf multibyte convertion leak [BZ#25691]
55f6e7
    
55f6e7
    Checked on x86_64-linux-gnu and i686-linux-gnu.
55f6e7
    
55f6e7
    (cherry picked from commit 910a835dc96c1f518ac2a6179fc622ba81ffb159)
55f6e7
55f6e7
diff --git a/stdio-common/Makefile b/stdio-common/Makefile
55f6e7
index a10f12ab3ccbd76e..51062a7dbf698931 100644
55f6e7
--- a/stdio-common/Makefile
55f6e7
+++ b/stdio-common/Makefile
55f6e7
@@ -63,6 +63,7 @@ tests := tstscanf test_rdwr test-popen tstgetln test-fseek \
55f6e7
 	 tst-vfprintf-mbs-prec \
55f6e7
 	 tst-scanf-round \
55f6e7
 	 tst-renameat2 \
55f6e7
+	 tst-printf-bz25691 \
55f6e7
 
55f6e7
 test-srcs = tst-unbputc tst-printf tst-printfsz-islongdouble
55f6e7
 
55f6e7
@@ -71,10 +72,12 @@ tests-special += $(objpfx)tst-unbputc.out $(objpfx)tst-printf.out \
55f6e7
 		 $(objpfx)tst-printf-bz18872-mem.out \
55f6e7
 		 $(objpfx)tst-setvbuf1-cmp.out \
55f6e7
 		 $(objpfx)tst-vfprintf-width-prec-mem.out \
55f6e7
-		 $(objpfx)tst-printfsz-islongdouble.out
55f6e7
+		 $(objpfx)tst-printfsz-islongdouble.out \
55f6e7
+		 $(objpfx)tst-printf-bz25691-mem.out
55f6e7
 generated += tst-printf-bz18872.c tst-printf-bz18872.mtrace \
55f6e7
 	     tst-printf-bz18872-mem.out \
55f6e7
-	     tst-vfprintf-width-prec.mtrace tst-vfprintf-width-prec-mem.out
55f6e7
+	     tst-vfprintf-width-prec.mtrace tst-vfprintf-width-prec-mem.out \
55f6e7
+	     tst-printf-bz25691.mtrace tst-printf-bz25691-mem.out
55f6e7
 endif
55f6e7
 
55f6e7
 include ../Rules
55f6e7
@@ -96,6 +99,8 @@ endif
55f6e7
 tst-printf-bz18872-ENV = MALLOC_TRACE=$(objpfx)tst-printf-bz18872.mtrace
55f6e7
 tst-vfprintf-width-prec-ENV = \
55f6e7
   MALLOC_TRACE=$(objpfx)tst-vfprintf-width-prec.mtrace
55f6e7
+tst-printf-bz25691-ENV = \
55f6e7
+  MALLOC_TRACE=$(objpfx)tst-printf-bz25691.mtrace
55f6e7
 
55f6e7
 $(objpfx)tst-unbputc.out: tst-unbputc.sh $(objpfx)tst-unbputc
55f6e7
 	$(SHELL) $< $(common-objpfx) '$(test-program-prefix)' > $@; \
55f6e7
diff --git a/stdio-common/tst-printf-bz25691.c b/stdio-common/tst-printf-bz25691.c
55f6e7
new file mode 100644
55f6e7
index 0000000000000000..37b30a3a8a7dc5e2
55f6e7
--- /dev/null
55f6e7
+++ b/stdio-common/tst-printf-bz25691.c
55f6e7
@@ -0,0 +1,108 @@
55f6e7
+/* Test for memory leak with large width (BZ#25691).
55f6e7
+   Copyright (C) 2020 Free Software Foundation, Inc.
55f6e7
+   This file is part of the GNU C Library.
55f6e7
+
55f6e7
+   The GNU C Library is free software; you can redistribute it and/or
55f6e7
+   modify it under the terms of the GNU Lesser General Public
55f6e7
+   License as published by the Free Software Foundation; either
55f6e7
+   version 2.1 of the License, or (at your option) any later version.
55f6e7
+
55f6e7
+   The GNU C Library is distributed in the hope that it will be useful,
55f6e7
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
55f6e7
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
55f6e7
+   Lesser General Public License for more details.
55f6e7
+
55f6e7
+   You should have received a copy of the GNU Lesser General Public
55f6e7
+   License along with the GNU C Library; if not, see
55f6e7
+   <https://www.gnu.org/licenses/>.  */
55f6e7
+
55f6e7
+#include <stdio.h>
55f6e7
+#include <stdlib.h>
55f6e7
+#include <string.h>
55f6e7
+#include <wchar.h>
55f6e7
+#include <stdint.h>
55f6e7
+#include <locale.h>
55f6e7
+
55f6e7
+#include <mcheck.h>
55f6e7
+#include <support/check.h>
55f6e7
+#include <support/support.h>
55f6e7
+
55f6e7
+static int
55f6e7
+do_test (void)
55f6e7
+{
55f6e7
+  mtrace ();
55f6e7
+
55f6e7
+  /* For 's' conversion specifier with 'l' modifier the array must be
55f6e7
+     converted to multibyte characters up to the precision specific
55f6e7
+     value.  */
55f6e7
+  {
55f6e7
+    /* The input size value is to force a heap allocation on temporary
55f6e7
+       buffer (in the old implementation).  */
55f6e7
+    const size_t winputsize = 64 * 1024 + 1;
55f6e7
+    wchar_t *winput = xmalloc (winputsize * sizeof (wchar_t));
55f6e7
+    wmemset (winput, L'a', winputsize - 1);
55f6e7
+    winput[winputsize - 1] = L'\0';
55f6e7
+
55f6e7
+    char result[9];
55f6e7
+    const char expected[] = "aaaaaaaa";
55f6e7
+    int ret;
55f6e7
+
55f6e7
+    ret = snprintf (result, sizeof (result), "%.65537ls", winput);
55f6e7
+    TEST_COMPARE (ret, winputsize - 1);
55f6e7
+    TEST_COMPARE_BLOB (result, sizeof (result), expected, sizeof (expected));
55f6e7
+
55f6e7
+    ret = snprintf (result, sizeof (result), "%ls", winput);
55f6e7
+    TEST_COMPARE (ret, winputsize - 1);
55f6e7
+    TEST_COMPARE_BLOB (result, sizeof (result), expected, sizeof (expected));
55f6e7
+
55f6e7
+    free (winput);
55f6e7
+  }
55f6e7
+
55f6e7
+  /* For 's' converstion specifier the array is interpreted as a multibyte
55f6e7
+     character sequence and converted to wide characters up to the precision
55f6e7
+     specific value.  */
55f6e7
+  {
55f6e7
+    /* The input size value is to force a heap allocation on temporary
55f6e7
+       buffer (in the old implementation).  */
55f6e7
+    const size_t mbssize = 32 * 1024;
55f6e7
+    char *mbs = xmalloc (mbssize);
55f6e7
+    memset (mbs, 'a', mbssize - 1);
55f6e7
+    mbs[mbssize - 1] = '\0';
55f6e7
+
55f6e7
+    const size_t expectedsize = 32 * 1024;
55f6e7
+    wchar_t *expected = xmalloc (expectedsize * sizeof (wchar_t));
55f6e7
+    wmemset (expected, L'a', expectedsize - 1);
55f6e7
+    expected[expectedsize-1] = L'\0';
55f6e7
+
55f6e7
+    const size_t resultsize = mbssize * sizeof (wchar_t);
55f6e7
+    wchar_t *result = xmalloc (resultsize);
55f6e7
+    int ret;
55f6e7
+
55f6e7
+    ret = swprintf (result, resultsize, L"%.65537s", mbs);
55f6e7
+    TEST_COMPARE (ret, mbssize - 1);
55f6e7
+    TEST_COMPARE_BLOB (result, (ret + 1) * sizeof (wchar_t),
55f6e7
+		       expected, expectedsize * sizeof (wchar_t));
55f6e7
+
55f6e7
+    ret = swprintf (result, resultsize, L"%1$.65537s", mbs);
55f6e7
+    TEST_COMPARE (ret, mbssize - 1);
55f6e7
+    TEST_COMPARE_BLOB (result, (ret + 1) * sizeof (wchar_t),
55f6e7
+		       expected, expectedsize * sizeof (wchar_t));
55f6e7
+
55f6e7
+    /* Same test, but with an invalid multibyte sequence.  */
55f6e7
+    mbs[mbssize - 2] = 0xff;
55f6e7
+
55f6e7
+    ret = swprintf (result, resultsize, L"%.65537s", mbs);
55f6e7
+    TEST_COMPARE (ret, -1);
55f6e7
+
55f6e7
+    ret = swprintf (result, resultsize, L"%1$.65537s", mbs);
55f6e7
+    TEST_COMPARE (ret, -1);
55f6e7
+
55f6e7
+    free (mbs);
55f6e7
+    free (result);
55f6e7
+    free (expected);
55f6e7
+  }
55f6e7
+
55f6e7
+  return 0;
55f6e7
+}
55f6e7
+
55f6e7
+#include <support/test-driver.c>