[PATCH] iconv: ISO-2022-CN-EXT: fix SS2 error position (bug 35025)

JT Wang ewilde1311@gmail.com
Sat Nov 21 08:51:37 GMT 2026


From: JT Wang <skyman-wftw@hotmail.com>

In the FROM loop, the SS2 (ESC N) handler advances inptr past the
escape before looking up the CNS 11643 plane 2 character.  When the
two payload bytes do not map to a character, cns11643l2_to_ucs4
leaves the pointer unchanged, so iconv() returns EILSEQ with the
input pointer two bytes past the ESC instead of at the start of the
four-byte sequence, as POSIX expects.

A caller that resumes conversion from the reported position then
decodes the payload bytes in the currently invoked character set:
with ASCII invoked, ESC N 0x7f 0x7f yields two U+007F characters
instead of an error.

Handle the four-byte sequence as one unit, as the SS3 branch in the
same function already does: pass a temporary pointer to the helper,
use STANDARD_FROM_LOOP_ERR_HANDLER (4), and advance inptr only on
success.  The non-ignore path now stops on the ESC.  With //IGNORE
the four bytes are still skipped from the ESC, as before.

Extend tst-iconv-iso-2022-cn-ext with FROM-direction regressions for
invalid and unmapped payloads with and without //IGNORE, a valid
plane 2 character, truncated SS2 sequences, and an E2BIG retry.

This was noted in bug 34774.

Signed-off-by: JT Wang <skyman-wftw@hotmail.com>
---
Tested on x86_64-linux-gnu with a default (-Werror) build:
make check subdirs=iconvdata passes.  With only iso-2022-cn-ext.c
reverted, the new tests report 12 failures, all in the input
position checks (inbuf and inbytesleft) of the six non-ignore
invalid-payload cases; the //IGNORE, valid, fragment and E2BIG cases
pass both before and after the fix.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=35025

 iconvdata/iso-2022-cn-ext.c           |   8 +-
 iconvdata/tst-iconv-iso-2022-cn-ext.c | 183 ++++++++++++++++++++++++++
 2 files changed, 188 insertions(+), 3 deletions(-)

diff --git a/iconvdata/iso-2022-cn-ext.c b/iconvdata/iso-2022-cn-ext.c
index cc8349a050..43765bb220 100644
--- a/iconvdata/iso-2022-cn-ext.c
+++ b/iconvdata/iso-2022-cn-ext.c
@@ -290,10 +290,12 @@ enum
 	   XXX We could test here whether the use of this character	      \
 	   set was announced.						      \
 	   XXX Currently GB7589 and GB13131 are not supported.  */	      \
-	inptr += 2;							      \
-	ch = cns11643l2_to_ucs4 (&inptr, 2, 0);				      \
+	const unsigned char *tmp = inptr + 2;				      \
+	ch = cns11643l2_to_ucs4 (&tmp, 2, 0);				      \
 	if (ch == __UNKNOWN_10646_CHAR)					      \
-	  STANDARD_FROM_LOOP_ERR_HANDLER (2);				      \
+	  STANDARD_FROM_LOOP_ERR_HANDLER (4);				      \
+	assert (tmp == inptr + 4);					      \
+	inptr += 4;							      \
       }									      \
     /* Note that we can assume here that at least 4 bytes are available if    \
        the first byte is ESC since otherwise the first if would have been     \
diff --git a/iconvdata/tst-iconv-iso-2022-cn-ext.c b/iconvdata/tst-iconv-iso-2022-cn-ext.c
index 39c34009e8..ca3eff1202 100644
--- a/iconvdata/tst-iconv-iso-2022-cn-ext.c
+++ b/iconvdata/tst-iconv-iso-2022-cn-ext.c
@@ -209,6 +209,181 @@ test_trailing_so_ignore (void)
   support_next_to_fault_free (&ntf);
 }
 
+/* FROM-direction regression tests for the SS2 (ESC N) error position:
+   on an invalid CNS 11643 plane 2 payload the non-ignore error path
+   must leave the input pointer on the ESC byte (not two bytes past
+   it), and //IGNORE must skip the whole four-byte SS2 sequence from
+   the ESC.  */
+
+/* Common runner: convert in/n with tocode, verify ret/errno, where the
+   input pointer stops, and the produced output bytes.  */
+static void
+run_ss2 (const char *tocode, const char *in, size_t n,
+	 size_t exp_consumed, const char *exp_out, size_t exp_outlen,
+	 int exp_eilseq)
+{
+  iconv_t cd = iconv_open (tocode, "ISO-2022-CN-EXT");
+  TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+  char out[16];
+  char *ip = (char *) in, *op = out;
+  size_t il = n, ol = sizeof out;
+  errno = 0;
+  if (exp_eilseq)
+    {
+      TEST_COMPARE (iconv (cd, &ip, &il, &op, &ol), (size_t) -1);
+      TEST_COMPARE (errno, EILSEQ);
+    }
+  else
+    TEST_COMPARE (iconv (cd, &ip, &il, &op, &ol), (size_t) 0);
+  TEST_COMPARE ((size_t) (ip - in), exp_consumed);
+  TEST_COMPARE (il, n - exp_consumed);
+  TEST_COMPARE ((size_t) (op - out), exp_outlen);
+  TEST_COMPARE_BLOB (out, op - out, exp_out, exp_outlen);
+  TEST_COMPARE (iconv_close (cd), 0);
+}
+
+/* Plane-2 designation ESC $ * H followed by an SS2 sequence.  */
+static const char ss2_desig[] = { 0x1b, '$', '*', 'H' };
+
+static void
+test_ss2_invalid_payload_plain (void)
+{
+  /* ESC N 7f 7f: both payload bytes fail the range check.  The error
+     must be reported with the input pointer on the ESC.  */
+  char in[9];
+  memcpy (in, ss2_desig, sizeof ss2_desig);
+  in[4] = 0x1b; in[5] = 0x4e; in[6] = 0x7f; in[7] = 0x7f; in[8] = 'B';
+  /* Before the fix the converter wrongly consumed ESC N, so the
+     invalid sequence was reported two bytes late.  */
+  run_ss2 ("UTF-8", in, sizeof in, 4, "", 0, 1);
+}
+
+static void
+test_ss2_invalid_payload_minimal (void)
+{
+  /* Same without the designation.  */
+  char in[] = { 'A', 0x1b, 0x4e, 0x7f, 0x7f, 'B' };
+  run_ss2 ("UTF-8", in, sizeof in, 1, "A", 1, 1);
+}
+
+static void
+test_ss2_invalid_payload_ignore (void)
+{
+  /* With //IGNORE the whole four-byte SS2 sequence is skipped from
+     the ESC and conversion continues.  The ret == -1 / EILSEQ outcome
+     with //IGNORE is the established exit-code contract
+     (STANDARD_FROM_LOOP_ERR_HANDLER keeps __GCONV_ILLEGAL_INPUT).  */
+  char in[9];
+  memcpy (in, ss2_desig, sizeof ss2_desig);
+  in[4] = 0x1b; in[5] = 0x4e; in[6] = 0x7f; in[7] = 0x7f; in[8] = 'B';
+  run_ss2 ("UTF-8//IGNORE", in, sizeof in, 9, "B", 1, 1);
+}
+
+static void
+test_ss2_valid (void)
+{
+  /* ESC N 21 21 is the first CNS 11643 plane 2 character, U+4E42.  */
+  char in[] = { 0x1b, '$', '*', 'H', 0x1b, 0x4e, 0x21, 0x21 };
+  run_ss2 ("UTF-8", in, sizeof in, 8, "\xe4\xb9\x82", 3, 0);
+}
+
+static void
+test_ss2_unmapped_payload (void)
+{
+  /* ESC N 72 45: index 0x1de2 is one past the last plane 2 table
+     entry, so the mapping is unknown and the error position is the
+     ESC.  */
+  char in[9];
+  memcpy (in, ss2_desig, sizeof ss2_desig);
+  in[4] = 0x1b; in[5] = 0x4e; in[6] = 0x72; in[7] = 0x45; in[8] = 'B';
+  run_ss2 ("UTF-8", in, sizeof in, 4, "", 0, 1);
+}
+
+static void
+test_ss2_fragment (void)
+{
+  /* A truncated SS2 sequence (fewer than four bytes from the ESC)
+     is incomplete input, not an illegal sequence: the pointer stops
+     on the ESC with EINVAL.  Two variants, differing only in the
+     truncated payload byte; the incompleteness check does not look
+     at the payload.  */
+  char in1[] = { 'A', 0x1b, 0x4e, 0x7f };
+  iconv_t cd = iconv_open ("UTF-8", "ISO-2022-CN-EXT");
+  TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+  char out[8];
+  char *ip = in1, *op = out;
+  size_t il = sizeof in1, ol = sizeof out;
+  errno = 0;
+  TEST_COMPARE (iconv (cd, &ip, &il, &op, &ol), (size_t) -1);
+  TEST_COMPARE (errno, EINVAL);
+  TEST_VERIFY (ip == in1 + 1);
+  TEST_COMPARE (il, (size_t) 3);
+  TEST_COMPARE_BLOB (out, op - out, "A", 1);
+  TEST_COMPARE (iconv_close (cd), 0);
+
+  char in2[] = { 'A', 0x1b, 0x4e, 0x21 };
+  cd = iconv_open ("UTF-8", "ISO-2022-CN-EXT");
+  TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+  ip = in2; op = out;
+  il = sizeof in2; ol = sizeof out;
+  errno = 0;
+  TEST_COMPARE (iconv (cd, &ip, &il, &op, &ol), (size_t) -1);
+  TEST_COMPARE (errno, EINVAL);
+  TEST_VERIFY (ip == in2 + 1);
+  TEST_COMPARE (il, (size_t) 3);
+  TEST_COMPARE_BLOB (out, op - out, "A", 1);
+  TEST_COMPARE (iconv_close (cd), 0);
+}
+
+static void
+test_ss2_payload_matrix (void)
+{
+  /* Parametrized invalid payloads covering the three rejection
+     paths of cns11643l2_to_ucs4: first-byte range check (7f 7f),
+     second-byte range check (21 7f), and index upper bound
+     (72 45).  */
+  static const char payloads[3][2] =
+    { { 0x7f, 0x7f }, { 0x21, 0x7f }, { 0x72, 0x45 } };
+  for (int i = 0; i < 3; i++)
+    {
+      char in[9];
+      memcpy (in, ss2_desig, sizeof ss2_desig);
+      in[4] = 0x1b; in[5] = 0x4e;
+      in[6] = payloads[i][0]; in[7] = payloads[i][1]; in[8] = 'B';
+      /* plain: error position is the ESC */
+      run_ss2 ("UTF-8", in, sizeof in, 4, "", 0, 1);
+      /* ignore: whole SS2 sequence skipped, 'B' still converts */
+      run_ss2 ("UTF-8//IGNORE", in, sizeof in, 9, "B", 1, 1);
+    }
+}
+
+static void
+test_ss2_state_recovery (void)
+{
+  /* E2BIG retry: with a one-byte output buffer the ASCII 'A'
+     converts and the SS2 character hits E2BIG; after enlarging the
+     output buffer the remaining input must convert without any
+     lost or duplicated bytes.  */
+  iconv_t cd = iconv_open ("UTF-8", "ISO-2022-CN-EXT");
+  TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+  char in[] = { 'A', 0x1b, '$', '*', 'H', 0x1b, 0x4e, 0x21, 0x21, 'B' };
+  char out[16];
+  char *ip = in, *op = out;
+  size_t il = sizeof in, ol = 1;
+  errno = 0;
+  TEST_COMPARE (iconv (cd, &ip, &il, &op, &ol), (size_t) -1);
+  TEST_COMPARE (errno, E2BIG);
+  TEST_COMPARE ((size_t) (op - out), (size_t) 1);
+  TEST_COMPARE_BLOB (out, op - out, "A", 1);
+  op = out;
+  ol = sizeof out;
+  errno = 0;
+  TEST_COMPARE (iconv (cd, &ip, &il, &op, &ol), (size_t) 0);
+  TEST_COMPARE (il, (size_t) 0);
+  TEST_COMPARE_BLOB (out, op - out, "\xe4\xb9\x82" "B", 4);
+  TEST_COMPARE (iconv_close (cd), 0);
+}
+
 static int
 do_test (void)
 {
@@ -301,6 +476,14 @@ do_test (void)
   test_caller_skip_no_underflow ();
   test_lone_so_tail_caller_skip ();
   test_designated_so ();
+  test_ss2_invalid_payload_plain ();
+  test_ss2_invalid_payload_minimal ();
+  test_ss2_invalid_payload_ignore ();
+  test_ss2_valid ();
+  test_ss2_unmapped_payload ();
+  test_ss2_fragment ();
+  test_ss2_payload_matrix ();
+  test_ss2_state_recovery ();
 
   return 0;
 }
-- 
2.43.0



More information about the Libc-alpha mailing list