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