94084c
commit f411207a833d0c49578ebe7062aee3660813ed5f
94084c
Author: Nikita Popov <npv1310@gmail.com>
94084c
Date:   Tue Nov 2 13:21:42 2021 +0500
94084c
94084c
    gconv: Do not emit spurious NUL character in ISO-2022-JP-3 (bug 28524)
94084c
    
94084c
    Bugfix 27256 has introduced another issue:
94084c
    In conversion from ISO-2022-JP-3 encoding, it is possible
94084c
    to force iconv to emit extra NUL character on internal state reset.
94084c
    To do this, it is sufficient to feed iconv with escape sequence
94084c
    which switches active character set.
94084c
    The simplified check 'data->__statep->__count != ASCII_set'
94084c
    introduced by the aforementioned bugfix picks that case and
94084c
    behaves as if '\0' character has been queued thus emitting it.
94084c
    
94084c
    To eliminate this issue, these steps are taken:
94084c
    * Restore original condition
94084c
    '(data->__statep->__count & ~7) != ASCII_set'.
94084c
    It is necessary since bits 0-2 may contain
94084c
    number of buffered input characters.
94084c
    * Check that queued character is not NUL.
94084c
    Similar step is taken for main conversion loop.
94084c
    
94084c
    Bundled test case follows following logic:
94084c
    * Try to convert ISO-2022-JP-3 escape sequence
94084c
    switching active character set
94084c
    * Reset internal state by providing NULL as input buffer
94084c
    * Ensure that nothing has been converted.
94084c
    
94084c
    Signed-off-by: Nikita Popov <npv1310@gmail.com>
94084c
    (cherry picked from commit ff012870b2c02a62598c04daa1e54632e020fd7d)
94084c
94084c
diff --git a/iconvdata/Makefile b/iconvdata/Makefile
94084c
index c216f959df1413f8..d5507a048c6a6508 100644
94084c
--- a/iconvdata/Makefile
94084c
+++ b/iconvdata/Makefile
94084c
@@ -1,4 +1,5 @@
94084c
 # Copyright (C) 1997-2021 Free Software Foundation, Inc.
94084c
+# Copyright (C) The GNU Toolchain Authors.
94084c
 # This file is part of the GNU C Library.
94084c
 
94084c
 # The GNU C Library is free software; you can redistribute it and/or
94084c
@@ -74,7 +75,7 @@ ifeq (yes,$(build-shared))
94084c
 tests = bug-iconv1 bug-iconv2 tst-loading tst-e2big tst-iconv4 bug-iconv4 \
94084c
 	tst-iconv6 bug-iconv5 bug-iconv6 tst-iconv7 bug-iconv8 bug-iconv9 \
94084c
 	bug-iconv10 bug-iconv11 bug-iconv12 tst-iconv-big5-hkscs-to-2ucs4 \
94084c
-	bug-iconv13 bug-iconv14
94084c
+	bug-iconv13 bug-iconv14 bug-iconv15
94084c
 ifeq ($(have-thread-library),yes)
94084c
 tests += bug-iconv3
94084c
 endif
94084c
@@ -327,6 +328,8 @@ $(objpfx)bug-iconv12.out: $(addprefix $(objpfx), $(gconv-modules)) \
94084c
 			  $(addprefix $(objpfx),$(modules.so))
94084c
 $(objpfx)bug-iconv14.out: $(addprefix $(objpfx), $(gconv-modules)) \
94084c
 			  $(addprefix $(objpfx),$(modules.so))
94084c
+$(objpfx)bug-iconv15.out: $(addprefix $(objpfx), $(gconv-modules)) \
94084c
+			  $(addprefix $(objpfx),$(modules.so))
94084c
 
94084c
 $(objpfx)iconv-test.out: run-iconv-test.sh \
94084c
 			 $(addprefix $(objpfx), $(gconv-modules)) \
94084c
diff --git a/iconvdata/bug-iconv15.c b/iconvdata/bug-iconv15.c
94084c
new file mode 100644
94084c
index 0000000000000000..cc04bd0313a68786
94084c
--- /dev/null
94084c
+++ b/iconvdata/bug-iconv15.c
94084c
@@ -0,0 +1,60 @@
94084c
+/* Bug 28524: Conversion from ISO-2022-JP-3 with iconv
94084c
+   may emit spurious NUL character on state reset.
94084c
+   Copyright (C) The GNU Toolchain Authors.
94084c
+   This file is part of the GNU C Library.
94084c
+
94084c
+   The GNU C Library is free software; you can redistribute it and/or
94084c
+   modify it under the terms of the GNU Lesser General Public
94084c
+   License as published by the Free Software Foundation; either
94084c
+   version 2.1 of the License, or (at your option) any later version.
94084c
+
94084c
+   The GNU C Library is distributed in the hope that it will be useful,
94084c
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
94084c
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
94084c
+   Lesser General Public License for more details.
94084c
+
94084c
+   You should have received a copy of the GNU Lesser General Public
94084c
+   License along with the GNU C Library; if not, see
94084c
+   <https://www.gnu.org/licenses/>.  */
94084c
+
94084c
+#include <stddef.h>
94084c
+#include <iconv.h>
94084c
+#include <support/check.h>
94084c
+
94084c
+static int
94084c
+do_test (void)
94084c
+{
94084c
+  char in[] = "\x1b(I";
94084c
+  char *inbuf = in;
94084c
+  size_t inleft = sizeof (in) - 1;
94084c
+  char out[1];
94084c
+  char *outbuf = out;
94084c
+  size_t outleft = sizeof (out);
94084c
+  iconv_t cd;
94084c
+
94084c
+  cd = iconv_open ("UTF8", "ISO-2022-JP-3");
94084c
+  TEST_VERIFY_EXIT (cd != (iconv_t) -1);
94084c
+
94084c
+  /* First call to iconv should alter internal state.
94084c
+     Now, JISX0201_Kana_set is selected and
94084c
+     state value != ASCII_set.  */
94084c
+  TEST_VERIFY (iconv (cd, &inbuf, &inleft, &outbuf, &outleft) != (size_t) -1);
94084c
+
94084c
+  /* No bytes should have been added to
94084c
+     the output buffer at this point.  */
94084c
+  TEST_VERIFY (outbuf == out);
94084c
+  TEST_VERIFY (outleft == sizeof (out));
94084c
+
94084c
+  /* Second call shall emit spurious NUL character in unpatched glibc.  */
94084c
+  TEST_VERIFY (iconv (cd, NULL, NULL, &outbuf, &outleft) != (size_t) -1);
94084c
+
94084c
+  /* No characters are expected to be produced.  */
94084c
+  TEST_VERIFY (outbuf == out);
94084c
+  TEST_VERIFY (outleft == sizeof (out));
94084c
+
94084c
+  TEST_VERIFY_EXIT (iconv_close (cd) != -1);
94084c
+
94084c
+  return 0;
94084c
+}
94084c
+
94084c
+#include <support/test-driver.c>
94084c
diff --git a/iconvdata/iso-2022-jp-3.c b/iconvdata/iso-2022-jp-3.c
94084c
index c8ba88cdc9fe9200..5fc0c0f7397935fe 100644
94084c
--- a/iconvdata/iso-2022-jp-3.c
94084c
+++ b/iconvdata/iso-2022-jp-3.c
94084c
@@ -1,5 +1,6 @@
94084c
 /* Conversion module for ISO-2022-JP-3.
94084c
    Copyright (C) 1998-2021 Free Software Foundation, Inc.
94084c
+   Copyright (C) The GNU Toolchain Authors.
94084c
    This file is part of the GNU C Library.
94084c
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998,
94084c
    and Bruno Haible <bruno@clisp.org>, 2002.
94084c
@@ -81,20 +82,31 @@ enum
94084c
    the output state to the initial state.  This has to be done during the
94084c
    flushing.  */
94084c
 #define EMIT_SHIFT_TO_INIT \
94084c
-  if (data->__statep->__count != ASCII_set)			      \
94084c
+  if ((data->__statep->__count & ~7) != ASCII_set)			      \
94084c
     {									      \
94084c
       if (FROM_DIRECTION)						      \
94084c
 	{								      \
94084c
-	  if (__glibc_likely (outbuf + 4 <= outend))			      \
94084c
+	  uint32_t ch = data->__statep->__count >> 6;			      \
94084c
+									      \
94084c
+	  if (__glibc_unlikely (ch != 0))				      \
94084c
 	    {								      \
94084c
-	      /* Write out the last character.  */			      \
94084c
-	      *((uint32_t *) outbuf) = data->__statep->__count >> 6;	      \
94084c
-	      outbuf += sizeof (uint32_t);				      \
94084c
-	      data->__statep->__count = ASCII_set;			\
94084c
+	      if (__glibc_likely (outbuf + 4 <= outend))		      \
94084c
+		{							      \
94084c
+		  /* Write out the last character.  */			      \
94084c
+		  put32u (outbuf, ch);					      \
94084c
+		  outbuf += 4;						      \
94084c
+		  data->__statep->__count &= 7;				      \
94084c
+		  data->__statep->__count |= ASCII_set;			      \
94084c
+		}							      \
94084c
+	      else							      \
94084c
+		/* We don't have enough room in the output buffer.  */	      \
94084c
+		status = __GCONV_FULL_OUTPUT;				      \
94084c
 	    }								      \
94084c
 	  else								      \
94084c
-	    /* We don't have enough room in the output buffer.  */	      \
94084c
-	    status = __GCONV_FULL_OUTPUT;				      \
94084c
+	    {								      \
94084c
+	      data->__statep->__count &= 7;				      \
94084c
+	      data->__statep->__count |= ASCII_set;			      \
94084c
+	    }								      \
94084c
 	}								      \
94084c
       else								      \
94084c
 	{								      \