446cf2
commit 7d88c6142c6efc160c0ee5e4f85cde382c072888
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Wed Jan 27 13:36:12 2021 +0100
446cf2
446cf2
    gconv: Fix assertion failure in ISO-2022-JP-3 module (bug 27256)
446cf2
    
446cf2
    The conversion loop to the internal encoding does not follow
446cf2
    the interface contract that __GCONV_FULL_OUTPUT is only returned
446cf2
    after the internal wchar_t buffer has been filled completely.  This
446cf2
    is enforced by the first of the two asserts in iconv/skeleton.c:
446cf2
    
446cf2
                  /* We must run out of output buffer space in this
446cf2
                     rerun.  */
446cf2
                  assert (outbuf == outerr);
446cf2
                  assert (nstatus == __GCONV_FULL_OUTPUT);
446cf2
    
446cf2
    This commit solves this issue by queuing a second wide character
446cf2
    which cannot be written immediately in the state variable, like
446cf2
    other converters already do (e.g., BIG5-HKSCS or TSCII).
446cf2
    
446cf2
    Reported-by: Tavis Ormandy <taviso@gmail.com>
446cf2
446cf2
Conflicts:
446cf2
	iconvdata/Makefile
446cf2
	  (Usual differences in backported tests.)
446cf2
446cf2
diff --git a/iconvdata/Makefile b/iconvdata/Makefile
446cf2
index a47a4c07cd2e3d1b..32656ad31d9b434b 100644
446cf2
--- a/iconvdata/Makefile
446cf2
+++ b/iconvdata/Makefile
446cf2
@@ -73,7 +73,7 @@ modules.so := $(addsuffix .so, $(modules))
446cf2
 ifeq (yes,$(build-shared))
446cf2
 tests = bug-iconv1 bug-iconv2 tst-loading tst-e2big tst-iconv4 bug-iconv4 \
446cf2
 	tst-iconv6 bug-iconv5 bug-iconv6 tst-iconv7 bug-iconv8 bug-iconv9 \
446cf2
-	bug-iconv10 bug-iconv11 bug-iconv12 bug-iconv13
446cf2
+	bug-iconv10 bug-iconv11 bug-iconv12 bug-iconv13 bug-iconv14
446cf2
 ifeq ($(have-thread-library),yes)
446cf2
 tests += bug-iconv3
446cf2
 endif
446cf2
@@ -316,6 +316,8 @@ $(objpfx)bug-iconv10.out: $(objpfx)gconv-modules \
446cf2
 			  $(addprefix $(objpfx),$(modules.so))
446cf2
 $(objpfx)bug-iconv12.out: $(objpfx)gconv-modules \
446cf2
 			  $(addprefix $(objpfx),$(modules.so))
446cf2
+$(objpfx)bug-iconv14.out: $(objpfx)gconv-modules \
446cf2
+			  $(addprefix $(objpfx),$(modules.so))
446cf2
 
446cf2
 $(objpfx)iconv-test.out: run-iconv-test.sh $(objpfx)gconv-modules \
446cf2
 			 $(addprefix $(objpfx),$(modules.so)) \
446cf2
diff --git a/iconvdata/bug-iconv14.c b/iconvdata/bug-iconv14.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..902f140fa949cbac
446cf2
--- /dev/null
446cf2
+++ b/iconvdata/bug-iconv14.c
446cf2
@@ -0,0 +1,127 @@
446cf2
+/* Assertion in ISO-2022-JP-3 due to two-character sequence (bug 27256).
446cf2
+   Copyright (C) 2021 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <iconv.h>
446cf2
+#include <string.h>
446cf2
+#include <errno.h>
446cf2
+#include <support/check.h>
446cf2
+
446cf2
+/* Use an escape sequence to return to the initial state.  */
446cf2
+static void
446cf2
+with_escape_sequence (void)
446cf2
+{
446cf2
+  iconv_t c = iconv_open ("UTF-8", "ISO-2022-JP-3");
446cf2
+  TEST_VERIFY_EXIT (c != (iconv_t) -1);
446cf2
+
446cf2
+  char in[] = "\e$(O+D\e(B";
446cf2
+  char *inbuf = in;
446cf2
+  size_t inleft = strlen (in);
446cf2
+  char out[3];                  /* Space for one output character.  */
446cf2
+  char *outbuf;
446cf2
+  size_t outleft;
446cf2
+
446cf2
+  outbuf = out;
446cf2
+  outleft = sizeof (out);
446cf2
+  TEST_COMPARE (iconv (c, &inbuf, &inleft, &outbuf, &outleft), (size_t) -1);
446cf2
+  TEST_COMPARE (errno, E2BIG);
446cf2
+  TEST_COMPARE (inleft, 3);
446cf2
+  TEST_COMPARE (inbuf - in, strlen (in) - 3);
446cf2
+  TEST_COMPARE (outleft, sizeof (out) - 2);
446cf2
+  TEST_COMPARE (outbuf - out, 2);
446cf2
+  TEST_COMPARE (out[0] & 0xff, 0xc3);
446cf2
+  TEST_COMPARE (out[1] & 0xff, 0xa6);
446cf2
+
446cf2
+  /* Return to the initial shift state, producing the pending
446cf2
+     character.  */
446cf2
+  outbuf = out;
446cf2
+  outleft = sizeof (out);
446cf2
+  TEST_COMPARE (iconv (c, &inbuf, &inleft, &outbuf, &outleft), 0);
446cf2
+  TEST_COMPARE (inleft, 0);
446cf2
+  TEST_COMPARE (inbuf - in, strlen (in));
446cf2
+  TEST_COMPARE (outleft, sizeof (out) - 2);
446cf2
+  TEST_COMPARE (outbuf - out, 2);
446cf2
+  TEST_COMPARE (out[0] & 0xff, 0xcc);
446cf2
+  TEST_COMPARE (out[1] & 0xff, 0x80);
446cf2
+
446cf2
+  /* Nothing should be flushed the second time.  */
446cf2
+  outbuf = out;
446cf2
+  outleft = sizeof (out);
446cf2
+  TEST_COMPARE (iconv (c, NULL, 0, &outbuf, &outleft), 0);
446cf2
+  TEST_COMPARE (outleft, sizeof (out));
446cf2
+  TEST_COMPARE (outbuf - out, 0);
446cf2
+  TEST_COMPARE (out[0] & 0xff, 0xcc);
446cf2
+  TEST_COMPARE (out[1] & 0xff, 0x80);
446cf2
+
446cf2
+  TEST_COMPARE (iconv_close (c), 0);
446cf2
+}
446cf2
+
446cf2
+/* Use an explicit flush to return to the initial state.  */
446cf2
+static void
446cf2
+with_flush (void)
446cf2
+{
446cf2
+  iconv_t c = iconv_open ("UTF-8", "ISO-2022-JP-3");
446cf2
+  TEST_VERIFY_EXIT (c != (iconv_t) -1);
446cf2
+
446cf2
+  char in[] = "\e$(O+D";
446cf2
+  char *inbuf = in;
446cf2
+  size_t inleft = strlen (in);
446cf2
+  char out[3];                  /* Space for one output character.  */
446cf2
+  char *outbuf;
446cf2
+  size_t outleft;
446cf2
+
446cf2
+  outbuf = out;
446cf2
+  outleft = sizeof (out);
446cf2
+  TEST_COMPARE (iconv (c, &inbuf, &inleft, &outbuf, &outleft), (size_t) -1);
446cf2
+  TEST_COMPARE (errno, E2BIG);
446cf2
+  TEST_COMPARE (inleft, 0);
446cf2
+  TEST_COMPARE (inbuf - in, strlen (in));
446cf2
+  TEST_COMPARE (outleft, sizeof (out) - 2);
446cf2
+  TEST_COMPARE (outbuf - out, 2);
446cf2
+  TEST_COMPARE (out[0] & 0xff, 0xc3);
446cf2
+  TEST_COMPARE (out[1] & 0xff, 0xa6);
446cf2
+
446cf2
+  /* Flush the pending character.  */
446cf2
+  outbuf = out;
446cf2
+  outleft = sizeof (out);
446cf2
+  TEST_COMPARE (iconv (c, NULL, 0, &outbuf, &outleft), 0);
446cf2
+  TEST_COMPARE (outleft, sizeof (out) - 2);
446cf2
+  TEST_COMPARE (outbuf - out, 2);
446cf2
+  TEST_COMPARE (out[0] & 0xff, 0xcc);
446cf2
+  TEST_COMPARE (out[1] & 0xff, 0x80);
446cf2
+
446cf2
+  /* Nothing should be flushed the second time.  */
446cf2
+  outbuf = out;
446cf2
+  outleft = sizeof (out);
446cf2
+  TEST_COMPARE (iconv (c, NULL, 0, &outbuf, &outleft), 0);
446cf2
+  TEST_COMPARE (outleft, sizeof (out));
446cf2
+  TEST_COMPARE (outbuf - out, 0);
446cf2
+  TEST_COMPARE (out[0] & 0xff, 0xcc);
446cf2
+  TEST_COMPARE (out[1] & 0xff, 0x80);
446cf2
+
446cf2
+  TEST_COMPARE (iconv_close (c), 0);
446cf2
+}
446cf2
+
446cf2
+static int
446cf2
+do_test (void)
446cf2
+{
446cf2
+  with_escape_sequence ();
446cf2
+  with_flush ();
446cf2
+  return 0;
446cf2
+}
446cf2
+
446cf2
+#include <support/test-driver.c>
446cf2
diff --git a/iconvdata/iso-2022-jp-3.c b/iconvdata/iso-2022-jp-3.c
446cf2
index de259580c3f378bb..047fab8e8dfbde7e 100644
446cf2
--- a/iconvdata/iso-2022-jp-3.c
446cf2
+++ b/iconvdata/iso-2022-jp-3.c
446cf2
@@ -67,23 +67,34 @@ enum
446cf2
   CURRENT_SEL_MASK = 7 << 3
446cf2
 };
446cf2
 
446cf2
-/* During UCS-4 to ISO-2022-JP-3 conversion, the COUNT element of the state
446cf2
-   also contains the last two bytes to be output, shifted by 6 bits, and a
446cf2
-   one-bit indicator whether they must be preceded by the shift sequence,
446cf2
-   in bit 22.  */
446cf2
+/* During UCS-4 to ISO-2022-JP-3 conversion, the COUNT element of the
446cf2
+   state also contains the last two bytes to be output, shifted by 6
446cf2
+   bits, and a one-bit indicator whether they must be preceded by the
446cf2
+   shift sequence, in bit 22.  During ISO-2022-JP-3 to UCS-4
446cf2
+   conversion, COUNT may also contain a non-zero pending wide
446cf2
+   character, shifted by six bits.  This happens for certain inputs in
446cf2
+   JISX0213_1_2004_set and JISX0213_2_set if the second wide character
446cf2
+   in a combining sequence cannot be written because the buffer is
446cf2
+   full.  */
446cf2
 
446cf2
 /* Since this is a stateful encoding we have to provide code which resets
446cf2
    the output state to the initial state.  This has to be done during the
446cf2
    flushing.  */
446cf2
 #define EMIT_SHIFT_TO_INIT \
446cf2
-  if ((data->__statep->__count & ~7) != ASCII_set)			      \
446cf2
+  if (data->__statep->__count != ASCII_set)			      \
446cf2
     {									      \
446cf2
       if (FROM_DIRECTION)						      \
446cf2
 	{								      \
446cf2
-	  /* It's easy, we don't have to emit anything, we just reset the     \
446cf2
-	     state for the input.  */					      \
446cf2
-	  data->__statep->__count &= 7;					      \
446cf2
-	  data->__statep->__count |= ASCII_set;				      \
446cf2
+	  if (__glibc_likely (outbuf + 4 <= outend))			      \
446cf2
+	    {								      \
446cf2
+	      /* Write out the last character.  */			      \
446cf2
+	      *((uint32_t *) outbuf) = data->__statep->__count >> 6;	      \
446cf2
+	      outbuf += sizeof (uint32_t);				      \
446cf2
+	      data->__statep->__count = ASCII_set;			\
446cf2
+	    }								      \
446cf2
+	  else								      \
446cf2
+	    /* We don't have enough room in the output buffer.  */	      \
446cf2
+	    status = __GCONV_FULL_OUTPUT;				      \
446cf2
 	}								      \
446cf2
       else								      \
446cf2
 	{								      \
446cf2
@@ -151,7 +162,21 @@ enum
446cf2
 #define LOOPFCT			FROM_LOOP
446cf2
 #define BODY \
446cf2
   {									      \
446cf2
-    uint32_t ch = *inptr;						      \
446cf2
+    uint32_t ch;							      \
446cf2
+									      \
446cf2
+    /* Output any pending character.  */				      \
446cf2
+    ch = set >> 6;							      \
446cf2
+    if (__glibc_unlikely (ch != 0))					      \
446cf2
+      {									      \
446cf2
+	put32 (outptr, ch);						      \
446cf2
+	outptr += 4;							      \
446cf2
+	/* Remove the pending character, but preserve state bits.  */	      \
446cf2
+	set &= (1 << 6) - 1;						      \
446cf2
+	continue;							      \
446cf2
+      }									      \
446cf2
+									      \
446cf2
+    /* Otherwise read the next input byte.  */				      \
446cf2
+    ch = *inptr;							      \
446cf2
 									      \
446cf2
     /* Recognize escape sequences.  */					      \
446cf2
     if (__glibc_unlikely (ch == ESC))					      \
446cf2
@@ -297,21 +322,25 @@ enum
446cf2
 	    uint32_t u1 = __jisx0213_to_ucs_combining[ch - 1][0];	      \
446cf2
 	    uint32_t u2 = __jisx0213_to_ucs_combining[ch - 1][1];	      \
446cf2
 									      \
446cf2
+	    inptr += 2;							      \
446cf2
+									      \
446cf2
+	    put32 (outptr, u1);						      \
446cf2
+	    outptr += 4;						      \
446cf2
+									      \
446cf2
 	    /* See whether we have room for two characters.  */		      \
446cf2
-	    if (outptr + 8 <= outend)					      \
446cf2
+	    if (outptr + 4 <= outend)					      \
446cf2
 	      {								      \
446cf2
-		inptr += 2;						      \
446cf2
-		put32 (outptr, u1);					      \
446cf2
-		outptr += 4;						      \
446cf2
 		put32 (outptr, u2);					      \
446cf2
 		outptr += 4;						      \
446cf2
 		continue;						      \
446cf2
 	      }								      \
446cf2
-	    else							      \
446cf2
-	      {								      \
446cf2
-		result = __GCONV_FULL_OUTPUT;				      \
446cf2
-		break;							      \
446cf2
-	      }								      \
446cf2
+									      \
446cf2
+	    /* Otherwise store only the first character now, and	      \
446cf2
+	       put the second one into the queue.  */			      \
446cf2
+	    set |= u2 << 6;						      \
446cf2
+	    /* Tell the caller why we terminate the loop.  */		      \
446cf2
+	    result = __GCONV_FULL_OUTPUT;				      \
446cf2
+	    break;							      \
446cf2
 	  }								      \
446cf2
 									      \
446cf2
 	inptr += 2;							      \