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