8ae002
From e4613df21e25e063d120ee23a650c65cd16df4be Mon Sep 17 00:00:00 2001
8ae002
From: Stefan Liebler <stli@linux.vnet.ibm.com>
8ae002
Date: Mon, 7 Nov 2016 17:30:22 +0100
8ae002
Subject: [PATCH 16/17] Fix ucs4le_internal_loop in error case. [BZ #19726]
8ae002
8ae002
Upstream commit 8f25676c83eef5c85db98f9cf027890fbe810447
8ae002
8ae002
When converting from UCS4LE to INTERNAL, the input-value is checked for a too
8ae002
large value and the iconv() call sets errno to EILSEQ. In this case the inbuf
8ae002
argument of the iconv() call should point to the invalid character, but it
8ae002
points to the beginning of the inbuf.
8ae002
Thus this patch updates the pointers inptrp and outptrp before returning in
8ae002
this error case.
8ae002
8ae002
This patch also adds a new testcase for this issue.
8ae002
The new test was tested on a s390, power, intel machine.
8ae002
8ae002
ChangeLog:
8ae002
8ae002
	[BZ #19726]
8ae002
	* iconv/gconv_simple.c (ucs4le_internal_loop): Update inptrp and
8ae002
	outptrp in case of an illegal input.
8ae002
	* iconv/tst-iconv6.c: New file.
8ae002
	* iconv/Makefile (tests): Add tst-iconv6.
8ae002
---
8ae002
 iconv/Makefile       |   2 +-
8ae002
 iconv/gconv_simple.c |   2 +
8ae002
 iconv/tst-iconv6.c   | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++
8ae002
 3 files changed, 120 insertions(+), 1 deletion(-)
8ae002
 create mode 100644 iconv/tst-iconv6.c
8ae002
8ae002
diff --git a/iconv/Makefile b/iconv/Makefile
8ae002
index 3e7f567..4d34c3f 100644
8ae002
--- a/iconv/Makefile
8ae002
+++ b/iconv/Makefile
8ae002
@@ -43,7 +43,7 @@ CFLAGS-charmap.c = -DCHARMAP_PATH='"$(i18ndir)/charmaps"' \
8ae002
 CFLAGS-linereader.c = -DNO_TRANSLITERATION
8ae002
 CFLAGS-simple-hash.c = -I../locale
8ae002
 
8ae002
-tests	= tst-iconv1 tst-iconv2 tst-iconv3 tst-iconv4 tst-iconv5
8ae002
+tests	= tst-iconv1 tst-iconv2 tst-iconv3 tst-iconv4 tst-iconv5 tst-iconv6
8ae002
 
8ae002
 others		= iconv_prog iconvconfig
8ae002
 install-others-programs	= $(inst_bindir)/iconv
8ae002
diff --git a/iconv/gconv_simple.c b/iconv/gconv_simple.c
8ae002
index 8697309..b9f846d 100644
8ae002
--- a/iconv/gconv_simple.c
8ae002
+++ b/iconv/gconv_simple.c
8ae002
@@ -634,6 +634,8 @@ ucs4le_internal_loop (struct __gconv_step *step,
8ae002
 	      continue;
8ae002
 	    }
8ae002
 
8ae002
+	  *inptrp = inptr;
8ae002
+	  *outptrp = outptr;
8ae002
 	  return __GCONV_ILLEGAL_INPUT;
8ae002
 	}
8ae002
 
8ae002
diff --git a/iconv/tst-iconv6.c b/iconv/tst-iconv6.c
8ae002
new file mode 100644
8ae002
index 0000000..57d7f38
8ae002
--- /dev/null
8ae002
+++ b/iconv/tst-iconv6.c
8ae002
@@ -0,0 +1,117 @@
8ae002
+/* Testing ucs4le_internal_loop() in gconv_simple.c.
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <stdio.h>
8ae002
+#include <errno.h>
8ae002
+#include <string.h>
8ae002
+#include <inttypes.h>
8ae002
+#include <iconv.h>
8ae002
+#include <byteswap.h>
8ae002
+
8ae002
+static int
8ae002
+do_test (void)
8ae002
+{
8ae002
+  iconv_t cd;
8ae002
+  char *inptr;
8ae002
+  size_t inlen;
8ae002
+  char *outptr;
8ae002
+  size_t outlen;
8ae002
+  size_t n;
8ae002
+  int e;
8ae002
+  int result = 0;
8ae002
+
8ae002
+#if __BYTE_ORDER == __BIG_ENDIAN
8ae002
+  /* On big-endian machines, ucs4le_internal_loop() swaps the bytes before
8ae002
+     error checking. Thus the input values has to be swapped.  */
8ae002
+# define VALUE(val) bswap_32 (val)
8ae002
+#else
8ae002
+# define VALUE(val) val
8ae002
+#endif
8ae002
+  uint32_t inbuf[3] = { VALUE (0x41), VALUE (0x80000000), VALUE (0x42) };
8ae002
+  uint32_t outbuf[3] = { 0, 0, 0 };
8ae002
+
8ae002
+  cd = iconv_open ("WCHAR_T", "UCS-4LE");
8ae002
+  if (cd == (iconv_t) -1)
8ae002
+    {
8ae002
+      printf ("cannot convert from UCS4LE to wchar_t: %m\n");
8ae002
+      return 1;
8ae002
+    }
8ae002
+
8ae002
+  inptr = (char *) inbuf;
8ae002
+  inlen = sizeof (inbuf);
8ae002
+  outptr = (char *) outbuf;
8ae002
+  outlen = sizeof (outbuf);
8ae002
+
8ae002
+  n = iconv (cd, &inptr, &inlen, &outptr, &outlen);
8ae002
+  e = errno;
8ae002
+
8ae002
+  if (n != (size_t) -1)
8ae002
+    {
8ae002
+      printf ("incorrect iconv() return value: %zd, expected -1\n", n);
8ae002
+      result = 1;
8ae002
+    }
8ae002
+
8ae002
+  if (e != EILSEQ)
8ae002
+    {
8ae002
+      printf ("incorrect error value: %s, expected %s\n",
8ae002
+	      strerror (e), strerror (EILSEQ));
8ae002
+      result = 1;
8ae002
+    }
8ae002
+
8ae002
+  if (inptr != (char *) &inbuf[1])
8ae002
+    {
8ae002
+      printf ("inptr=0x%p does not point to invalid character! Expected=0x%p\n"
8ae002
+	      , inptr, &inbuf[1]);
8ae002
+      result = 1;
8ae002
+    }
8ae002
+
8ae002
+  if (inlen != sizeof (inbuf) - sizeof (uint32_t))
8ae002
+    {
8ae002
+      printf ("inlen=%zd != %zd\n"
8ae002
+	      , inlen, sizeof (inbuf) - sizeof (uint32_t));
8ae002
+      result = 1;
8ae002
+    }
8ae002
+
8ae002
+  if (outptr != (char *) &outbuf[1])
8ae002
+    {
8ae002
+      printf ("outptr=0x%p does not point to invalid character in inbuf! "
8ae002
+	      "Expected=0x%p\n"
8ae002
+	      , outptr, &outbuf[1]);
8ae002
+      result = 1;
8ae002
+    }
8ae002
+
8ae002
+  if (outlen != sizeof (inbuf) - sizeof (uint32_t))
8ae002
+    {
8ae002
+      printf ("outlen=%zd != %zd\n"
8ae002
+	      , outlen, sizeof (outbuf) - sizeof (uint32_t));
8ae002
+      result = 1;
8ae002
+    }
8ae002
+
8ae002
+  if (outbuf[0] != 0x41 || outbuf[1] != 0 || outbuf[2] != 0)
8ae002
+    {
8ae002
+      puts ("Characters conversion is incorrect!");
8ae002
+      result = 1;
8ae002
+    }
8ae002
+
8ae002
+  iconv_close (cd);
8ae002
+
8ae002
+  return result;
8ae002
+}
8ae002
+
8ae002
+#define TEST_FUNCTION do_test ()
8ae002
+#include "../test-skeleton.c"
8ae002
-- 
8ae002
1.8.3.1
8ae002