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