[PATCH] iconvdata: Use pending base character state in IBM1390, IBM1399 encoders

JT Wang ewilde1311@gmail.com
Mon Nov 23 14:08:14 GMT 2026


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

A combining base character that ends an input buffer is currently
encoded as an individual character, so a combining character arriving
in the next iconv call cannot combine with it (the long-standing XXX
comment in SUB_COMBINED_UCS_TO_IBM13XX).  As Andreas Schwab put it on
libc-alpha, the character is "consumed but encoded as individual
character instead of being combined with the one from the next
buffer".  For example, converting L"\u00e6" in one call and L"\u0300"
in the next currently produces the standalone encodings (SO 0xd6 0x7b,
then 0xea 0x51) instead of the combined character (SO 0xec 0xc3).

Follow the example in iso-2022-jp-3.c and store the base character in
the __count state variable, in bits 7 and above, as the FROM direction
already does since the fix for bug 33980.  The pending character
combines with the first code point of the next buffer if possible, and
is written out on its own before that character otherwise.  Callers
now have to flush the descriptor (iconv with a NULL input buffer
argument) to obtain a trailing lone combining base character; without
the flush, that character is not written, and previously only the
closing SI could be lost this way.  EUC-JISX0213 and ISO-2022-JP-3
have the same property.  The flush writes the pending character and
the shift back to single-byte mode atomically, following the
need-then-write discipline.  Loop-internal output keeps the existing
two-phase SO/double-byte pattern, so the MIN_NEEDED/MAX_NEEDED macros
are unchanged.  The change is effective for the two HAS_COMBINED
codecs only; IBM1364, IBM1371 and IBM1388 are unchanged.

The new test covers cross-call combination, the deferral trigger,
error and retry paths, flush capacity scans in both modes, the
discarding reset, and multi-step chains, for both charsets.  The full
iconvdata test suite passes, with a default -Werror build.

This resolves the XXX comment.

Signed-off-by: JT Wang <skyman-wftw@hotmail.com>
---
This addresses the remaining XXX comment in SUB_COMBINED_UCS_TO_IBM13XX
that Andreas described in the bug 34914 thread:
<https://sourceware.org/pipermail/libc-alpha/2026-November/182143.html>

Tested on x86_64 (default -Werror build): the new test passes, and the
full iconvdata test suite has no regressions.  With ibm1364.c reverted to
master, the new test fails (lost combinations and missing flush output).

 iconvdata/Makefile                    |   3 +
 iconvdata/ibm1364.c                   | 188 ++++++++--
 iconvdata/tst-iconv-ibm1390-pending.c | 504 ++++++++++++++++++++++++++
 3 files changed, 672 insertions(+), 23 deletions(-)
 create mode 100644 iconvdata/tst-iconv-ibm1390-pending.c

diff --git a/iconvdata/Makefile b/iconvdata/Makefile
index 2abafa8..4e0de0c 100644
--- a/iconvdata/Makefile
+++ b/iconvdata/Makefile
@@ -90,6 +90,7 @@ tests = \
   tst-e2big \
   tst-iconv-big5-hkscs-to-2ucs4 \
   tst-iconv-ibm1390 \
+  tst-iconv-ibm1390-pending \
   tst-iconv-iso-2022-cn-ext \
   tst-iconv4 \
   tst-iconv6 \
@@ -359,6 +360,8 @@ $(objpfx)tst-jisx0213-progress.out: \
   $(addprefix $(objpfx), $(gconv-modules)) $(addprefix $(objpfx),$(modules.so))
 $(objpfx)tst-iconv-ibm1390.out: $(addprefix $(objpfx), $(gconv-modules)) \
 				$(addprefix $(objpfx),$(modules.so))
+$(objpfx)tst-iconv-ibm1390-pending.out: $(addprefix $(objpfx), $(gconv-modules)) \
+	$(addprefix $(objpfx),$(modules.so))
 
 $(objpfx)iconv-test.out: run-iconv-test.sh \
 			 $(addprefix $(objpfx), $(gconv-modules)) \
diff --git a/iconvdata/ibm1364.c b/iconvdata/ibm1364.c
index 6b99d3b..49be5c5 100644
--- a/iconvdata/ibm1364.c
+++ b/iconvdata/ibm1364.c
@@ -66,9 +66,12 @@
 
 
 /* Since this is a stateful encoding we have to provide code which resets
-   the output state to the initial state.  This has to be done during the
-   flushing.  For the to-internal direction (FROM_DIRECTION is true),
-   there may be a pending character that needs flushing.  */
+   the output state to the initial state.  This has to be done during
+   the flushing.  In either direction there may be a pending character
+   that needs flushing: the second wide character of a combining
+   sequence in the to-internal direction, and a combining base
+   character that ended the input buffer in the from-internal
+   direction.  */
 #define EMIT_SHIFT_TO_INIT \
   if ((data->__statep->__count & ~7) != sb)				      \
     {									      \
@@ -91,19 +94,7 @@
 	    data->__statep->__count &= 7;				      \
 	}								      \
       else								      \
-	{								      \
-	  /* We are not in the initial state.  To switch back we have	      \
-	     to emit `SI'.  */						      \
-	  if (__glibc_unlikely (outbuf >= outend))			      \
-	    /* We don't have enough room in the output buffer.  */	      \
-	    status = __GCONV_FULL_OUTPUT;				      \
-	  else								      \
-	    {								      \
-	      /* Write out the shift sequence.  */			      \
-	      *outbuf++ = SI;						      \
-	      data->__statep->__count &= 7;				      \
-	    }								      \
-	}								      \
+	TO_EMIT_SHIFT_TO_INIT						      \
     }
 
 
@@ -118,13 +109,76 @@
 
 /* Current codeset type.  The bit is stored in the __count variable of
    the conversion state.  If the db bit is set, bit 7 and above store
-   a pending UCS-4 code point if non-zero.  */
+   a pending UCS-4 code point if non-zero.  In the FROM direction
+   this happens when the second wide character of a combining
+   sequence does not fit into the output buffer.  In the TO direction
+   (HAS_COMBINED only), bit 7 and above store a combining base
+   character that ended the input buffer and may combine with the
+   first character of the next buffer, in either mode.  */
 enum
 {
   sb = 0,			/* Single byte mode.  */
   db = 64			/* Double byte mode.  */
 };
 
+/* Look up the standalone double-byte encoding of a combining base
+   character.  Combining base characters always have such a mapping
+   in the generated tables.  */
+#ifdef HAS_COMBINED
+# define LOOKUP_UCS4_TO_DB(ch, cp)					      \
+  do									      \
+    {									      \
+      const struct gap *rp_ = UCS4_TO_DB_IDX;				      \
+      while (ch > rp_->end)						      \
+	++rp_;								      \
+      assert (ch >= rp_->start && UCS4_TO_DB[ch + rp_->idx][0] != '\0');      \
+      cp = UCS4_TO_DB[ch + rp_->idx];					      \
+    }									      \
+  while (0)
+
+/* Write out a pending combining base character (if any) and the
+   shift back to single-byte mode, atomically.  */
+# define TO_EMIT_SHIFT_TO_INIT						      \
+  {									      \
+    uint32_t pendc = (uint32_t) data->__statep->__count >> 7;		      \
+    size_t need = pendc != 0						      \
+	  ? ((data->__statep->__count & db) == 0 ? 4 : 3) : 1;		      \
+    if (__glibc_unlikely ((size_t) (outend - outbuf) < need))		      \
+      /* We don't have enough room in the output buffer.  */		      \
+      status = __GCONV_FULL_OUTPUT;					      \
+    else								      \
+      {									      \
+	if (pendc != 0)							      \
+	  {								      \
+	    if ((data->__statep->__count & db) == 0)			      \
+	      *outbuf++ = SO;						      \
+	    const char *cp;						      \
+	    LOOKUP_UCS4_TO_DB (pendc, cp);				      \
+	    *outbuf++ = cp[0];						      \
+	    *outbuf++ = cp[1];						      \
+	  }								      \
+	/* Write out the shift sequence.  */				      \
+	*outbuf++ = SI;							      \
+	data->__statep->__count &= 7;					      \
+      }									      \
+  }
+#else
+# define TO_EMIT_SHIFT_TO_INIT						      \
+  {									      \
+    /* We are not in the initial state.  To switch back we have		      \
+       to emit `SI'.  */						      \
+    if (__glibc_unlikely (outbuf >= outend))				      \
+      /* We don't have enough room in the output buffer.  */		      \
+      status = __GCONV_FULL_OUTPUT;					      \
+    else								      \
+      {									      \
+	/* Write out the shift sequence.  */				      \
+	*outbuf++ = SI;							      \
+	data->__statep->__count &= 7;					      \
+      }									      \
+  }
+#endif
+
 
 /* Subroutine to write out converted UCS4 from IBM-13XX.  */
 #ifdef HAS_COMBINED
@@ -285,10 +339,20 @@ enum
     const struct combine *cmbp = UCS4_COMB_TO_DB;			      \
     while (cmbp->res1 < ch)						      \
       ++cmbp;								      \
-    /* XXX if last char is beginning of combining store in state */	      \
-    if (cmbp->res1 == ch && inend - inptr >= 8)				      \
+    /* See if the input is part of a combined character.  If the	      \
+       next code point is not in this buffer, store the combining	      \
+       base character in the state, to be combined with (or		      \
+       written out before) the first character of the next		      \
+       buffer.  */							      \
+    if (cmbp->res1 == ch)						      \
       {									      \
-	/* See if input is part of a combined character.  */		      \
+	if (inend - inptr < 8)						      \
+	  {								      \
+	    assert (ch <= 0xffff);					      \
+	    pending = ch;						      \
+	    inptr += 4;							      \
+	    continue;							      \
+	  }								      \
 	uint32_t ch_next = get32 (inptr + 4);				      \
 	while (cmbp->res2 != ch_next)					      \
 	  {								      \
@@ -323,6 +387,71 @@ enum
 # define SUB_COMBINED_UCS_TO_IBM13XX
 #endif /* HAS_COMBINED */
 
+/* The pending combining base character relies on UPDATE_PARAMS for
+   state saving, as in iso-2022-jp-3.c.  The _single loop (used for
+   consume_incomplete operation) is not supported because these
+   charsets are not used as locale charsets.  */
+#ifdef HAS_COMBINED
+# define TO_PENDING_HANDLING						      \
+  {									      \
+    if (pending != 0)							      \
+      {									      \
+	/* See if the pending base character combines with the current	      \
+	   one.  */							      \
+	const struct combine *cmbp = UCS4_COMB_TO_DB;			      \
+	while (cmbp->res1 < pending)					      \
+	  ++cmbp;							      \
+	while (cmbp->res2 != ch)					      \
+	  {								      \
+	    ++cmbp;							      \
+	    if (cmbp->res1 != pending)					      \
+	      goto not_pending_combined;				      \
+	  }								      \
+	/* It is a combined character.  First make sure we are in	      \
+	   double byte mode.  */					      \
+	if (curcs == sb)						      \
+	  {								      \
+	    /* We know there is room for at least one byte.  */		      \
+	    *outptr++ = SO;						      \
+	    curcs = db;							      \
+	  }								      \
+	if (__glibc_unlikely (outend - outptr < 2))			      \
+	  {								      \
+	    result = __GCONV_FULL_OUTPUT;				      \
+	    break;							      \
+	  }								      \
+	*outptr++ = cmbp->ch[0];					      \
+	*outptr++ = cmbp->ch[1];					      \
+	pending = 0;							      \
+	inptr += 4;							      \
+	continue;							      \
+									      \
+      not_pending_combined:;						      \
+	/* Write out the pending base character on its own.  The	      \
+	   current character is processed in the next iteration.  */	      \
+	if (curcs == sb)						      \
+	  {								      \
+	    /* We know there is room for at least one byte.  */		      \
+	    *outptr++ = SO;						      \
+	    curcs = db;							      \
+	  }								      \
+	if (__glibc_unlikely (outend - outptr < 2))			      \
+	  {								      \
+	    result = __GCONV_FULL_OUTPUT;				      \
+	    break;							      \
+	  }								      \
+	const char *cp;							      \
+	LOOKUP_UCS4_TO_DB (pending, cp);				      \
+	*outptr++ = cp[0];						      \
+	*outptr++ = cp[1];						      \
+	pending = 0;							      \
+	continue;							      \
+      }									      \
+  }
+#else
+# define TO_PENDING_HANDLING
+#endif
+
 
 /* Next, define the other direction.  */
 #define MIN_NEEDED_INPUT	MIN_NEEDED_TO
@@ -334,6 +463,7 @@ enum
   {									      \
     uint32_t ch = get32 (inptr);					      \
 									      \
+    TO_PENDING_HANDLING;						      \
     if (__glibc_unlikely (ch >= UCS_LIMIT))				      \
       {									      \
 	UNICODE_TAG_HANDLER (ch, 4);					      \
@@ -418,9 +548,21 @@ enum
   }
 #define LOOP_NEED_FLAGS
 #define EXTRA_LOOP_DECLS	, int *curcsp
-#define INIT_PARAMS		int curcs = *curcsp & ~7
-#define REINIT_PARAMS		curcs = *curcsp & ~7
-#define UPDATE_PARAMS		*curcsp = curcs
+#ifdef HAS_COMBINED
+# define INIT_PARAMS		int curcs = *curcsp & db;		      \
+				uint32_t pending = (uint32_t) *curcsp >> 7
+# define REINIT_PARAMS		do					      \
+				  {					      \
+				    curcs = *curcsp & db;		      \
+				    pending = (uint32_t) *curcsp >> 7;	      \
+				  }					      \
+				while (0)
+# define UPDATE_PARAMS		*curcsp = curcs | (pending << 7)
+#else
+# define INIT_PARAMS		int curcs = *curcsp & ~7
+# define REINIT_PARAMS		curcs = *curcsp & ~7
+# define UPDATE_PARAMS		*curcsp = curcs
+#endif
 #include <iconv/loop.c>
 
 /* Now define the toplevel functions.  */
diff --git a/iconvdata/tst-iconv-ibm1390-pending.c b/iconvdata/tst-iconv-ibm1390-pending.c
new file mode 100644
index 0000000..c9e09dd
--- /dev/null
+++ b/iconvdata/tst-iconv-ibm1390-pending.c
@@ -0,0 +1,504 @@
+/* Check combining base character state handling in the IBM1390 and
+   IBM1399 encoders (follow-up to bug 34914).
+   Copyright The GNU Toolchain Authors.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* A combining base character that ends an input buffer is stored in
+   the conversion state.  It combines with a suitable combining
+   character in the next iconv call, is written out on its own before
+   an unsuitable character, and is flushed by the state reset.  */
+
+#include <errno.h>
+#include <iconv.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <support/check.h>
+
+/* Fills the whole output buffer (and a few bytes beyond the declared
+   capacity) with a sentinel, to catch both writes past the buffer and
+   writes that the return value does not account for.  */
+static void
+fill_sentinel (unsigned char *buf, size_t cap)
+{
+  memset (buf, 0x55, cap + 8);
+}
+
+static void
+check_sentinel (const char *tag, const unsigned char *buf, size_t cap,
+		size_t outlen)
+{
+  for (size_t i = outlen; i < cap + 8; i++)
+    if (buf[i] != 0x55)
+      FAIL_EXIT1 ("%s: wrote to the output buffer at offset %zu without "
+		  "reporting it", tag, i);
+}
+
+struct result
+{
+  size_t ret;
+  int err;
+  size_t inleft;
+  size_t outlen;
+  unsigned char out[64];
+};
+
+static void
+clear (struct result *r)
+{
+  memset (r, 0, sizeof (*r));
+}
+
+static void
+check_conv (const char *tag, struct result *r, ssize_t ret, int err,
+	    size_t inleft, const unsigned char *out, size_t outlen)
+{
+  if (r->ret != (size_t) ret || r->err != err || r->inleft != inleft
+      || r->outlen != outlen || memcmp (r->out, out, outlen) != 0)
+    {
+      printf ("%s: unexpected conversion result\n", tag);
+      TEST_COMPARE (r->ret, (size_t) ret);
+      TEST_COMPARE (r->err, err);
+      TEST_COMPARE (r->inleft, inleft);
+      TEST_COMPARE (r->outlen, outlen);
+      TEST_COMPARE_BLOB (r->out, r->outlen, out, outlen);
+    }
+}
+
+/* One iconv call with WCHAR_T input built from wide characters.  */
+static struct result
+call_wide (iconv_t cd, const char *tag, const wchar_t *w, size_t n,
+	   size_t outcap)
+{
+  unsigned char inbuf[64];
+  TEST_VERIFY_EXIT (n * sizeof (wchar_t) <= sizeof (inbuf));
+  memcpy (inbuf, w, n * sizeof (wchar_t));
+  char *inp = (char *) inbuf;
+  size_t inleft = n * sizeof (wchar_t);
+  unsigned char outbuf[64];
+  char *outp = (char *) outbuf;
+  size_t outleft = outcap;
+  struct result r;
+  clear (&r);
+  fill_sentinel (outbuf, outcap);
+  errno = 0;
+  r.ret = iconv (cd, &inp, &inleft, &outp, &outleft);
+  r.err = errno;
+  r.inleft = inleft;
+  r.outlen = outcap - outleft;
+  memcpy (r.out, outbuf, r.outlen);
+  check_sentinel (tag, outbuf, outcap, r.outlen);
+  return r;
+}
+
+/* One iconv call with raw input bytes (native byte order).  */
+static struct result
+call_raw (iconv_t cd, const char *tag, const void *bytes, size_t len,
+	  size_t outcap)
+{
+  char *inp = (char *) bytes;
+  size_t inleft = len;
+  unsigned char outbuf[64];
+  char *outp = (char *) outbuf;
+  size_t outleft = outcap;
+  struct result r;
+  clear (&r);
+  fill_sentinel (outbuf, outcap);
+  errno = 0;
+  r.ret = iconv (cd, &inp, &inleft, &outp, &outleft);
+  r.err = errno;
+  r.inleft = inleft;
+  r.outlen = outcap - outleft;
+  memcpy (r.out, outbuf, r.outlen);
+  check_sentinel (tag, outbuf, outcap, r.outlen);
+  return r;
+}
+
+static struct result
+flush_cd (iconv_t cd, const char *tag, size_t outcap)
+{
+  unsigned char outbuf[64];
+  char *outp = (char *) outbuf;
+  size_t outleft = outcap;
+  struct result r;
+  clear (&r);
+  fill_sentinel (outbuf, outcap);
+  errno = 0;
+  r.ret = iconv (cd, NULL, NULL, &outp, &outleft);
+  r.err = errno;
+  r.inleft = 0;
+  r.outlen = outcap - outleft;
+  memcpy (r.out, outbuf, r.outlen);
+  check_sentinel (tag, outbuf, outcap, r.outlen);
+  return r;
+}
+
+/* Code point bytes in native byte order.  */
+static void
+cp_bytes (uint32_t cp, unsigned char b[4])
+{
+  memcpy (b, &cp, 4);
+}
+
+static void
+test_pending (const char *coding, unsigned char a_byte)
+{
+  /* A lone combining base character is consumed with no output and
+     kept in the state.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "lone base", L"\u00e6", 1, 16);
+    check_conv ("lone base: consumed, no output", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    /* The flush writes SO, the base character, and SI.  */
+    r = flush_cd (cd, "flush (sb)", 16);
+    check_conv ("flush after lone base (sb)", &r, 0, 0, 0,
+		(const unsigned char *) "\x0e\xd6\x7b\x0f", 4);
+    /* A second flush produces nothing.  */
+    r = flush_cd (cd, "second flush", 16);
+    check_conv ("second flush", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  /* A combining character in the next call combines with the stored
+     base character.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "cross-call first", L"\u00e6", 1, 16);
+    check_conv ("cross-call: first call", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    r = call_wide (cd, "cross-call second", L"\u0300", 1, 16);
+    check_conv ("cross-call: combines", &r, 0, 0, 0,
+		(const unsigned char *) "\x0e\xec\xc3", 3);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  /* A complete pair in one buffer is not deferred: with minimal
+     output capacity, only SO is written and nothing is consumed.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "same-buffer cap 1",
+				 L"\u00e6\u0300", 2, 1);
+    check_conv ("same-buffer, cap 1: E2BIG, only SO, nothing consumed",
+		&r, -1, E2BIG, 8, (const unsigned char *) "\x0e", 1);
+    /* Resending the complete pair on the same descriptor does not
+       write another SO.  */
+    r = call_wide (cd, "same-buffer retry", L"\u00e6\u0300", 2, 2);
+    check_conv ("same-buffer retry: no repeated SO", &r, 0, 0, 0,
+		(const unsigned char *) "\xec\xc3", 2);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "same-buffer one call",
+				 L"\u00e6\u0300", 2, 4);
+    check_conv ("same-buffer one call", &r, 0, 0, 0,
+		(const unsigned char *) "\x0e\xec\xc3", 3);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  /* The pending base character is written out before an ordinary
+     single-byte character.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "pending ordinary setup",
+				 L"\u00e6", 1, 16);
+    check_conv ("pending + ordinary: setup", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    unsigned char expect[] = { 0x0e, 0xd6, 0x7b, 0x0f, a_byte };
+    r = call_wide (cd, "pending ordinary", L"a", 1, 16);
+    check_conv ("pending + ordinary: base then SI then 'a'", &r, 0, 0, 0,
+		expect, 5);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  /* The pending base character is written out before another base
+     character, which is itself stored in the state.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "two bases setup", L"\u00e6", 1, 16);
+    check_conv ("two bases: setup", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    r = call_wide (cd, "two bases second", L"\u304b", 1, 16);
+    check_conv ("two bases: first written, second pending", &r, 0, 0, 0,
+		(const unsigned char *) "\x0e\xd6\x7b", 3);
+    r = flush_cd (cd, "two bases flush", 16);
+    check_conv ("two bases: flush second (db)", &r, 0, 0, 0,
+		(const unsigned char *) "\x44\x86\x0f", 3);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  /* A combining character without standalone mapping after a pending
+     base character: the base character is written out, then the
+     error is reported without consuming anything.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "EILSEQ setup", L"\u00e6", 1, 16);
+    check_conv ("EILSEQ: setup", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    r = call_wide (cd, "EILSEQ mark", L"\u309a", 1, 16);
+    check_conv ("EILSEQ: pending out, then error, nothing consumed",
+		&r, -1, EILSEQ, 4,
+		(const unsigned char *) "\x0e\xd6\x7b", 3);
+    /* Conversion continues normally after the error.  */
+    unsigned char expect[] = { 0x0f, a_byte };
+    r = call_wide (cd, "EILSEQ continue", L"a", 1, 16);
+    check_conv ("EILSEQ: continues without repeating the base", &r, 0,
+		0, 0, expect, 2);
+    r = flush_cd (cd, "EILSEQ final flush", 16);
+    check_conv ("EILSEQ: final flush is empty", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+  /* Contrast: a matching mark combines.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "combine setup", L"\u304b", 1, 16);
+    check_conv ("combine contrast: setup", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    r = call_wide (cd, "combine mark", L"\u309a", 1, 16);
+    check_conv ("combine contrast: combines", &r, 0, 0, 0,
+		(const unsigned char *) "\x0e\xec\xb5", 3);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+  /* Contrast: an unmatched mark with a standalone mapping.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "unmatched setup", L"\u304b", 1, 16);
+    check_conv ("unmatched mark: setup", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    r = call_wide (cd, "unmatched mark", L"\u0300", 1, 16);
+    check_conv ("unmatched mark: base, then mark mapping", &r, 0, 0, 0,
+		(const unsigned char *) "\x0e\x44\x86\xea\x51", 5);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  /* An illegal code point after a pending base character: the base
+     character is written out before the error is reported.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "illegal setup", L"\u00e6", 1, 16);
+    check_conv ("illegal: setup", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    unsigned char bad[4];
+    cp_bytes (UINT32_MAX, bad);
+    r = call_raw (cd, "illegal code point", bad, 4, 16);
+    check_conv ("illegal: pending out, then EILSEQ", &r, -1, EILSEQ, 4,
+		(const unsigned char *) "\x0e\xd6\x7b", 3);
+    unsigned char expect[] = { 0x0f, a_byte };
+    r = call_wide (cd, "illegal continue", L"a", 1, 16);
+    check_conv ("illegal: continues without repeating the base", &r, 0,
+		0, 0, expect, 2);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  /* A trailing fragment of the combining character leaves only the
+     fragment unconsumed; resending the complete code point combines
+     it with the stored base character.  */
+  for (int k = 1; k <= 3; k++)
+    {
+      iconv_t cd = iconv_open (coding, "WCHAR_T");
+      TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+      unsigned char mark[4];
+      cp_bytes (0x300, mark);
+      unsigned char inbuf[8];
+      cp_bytes (0xe6, inbuf);
+      memcpy (inbuf + 4, mark, k);
+      struct result r = call_raw (cd, "fragment", inbuf, 4 + k, 16);
+      char tag[64];
+      snprintf (tag, sizeof tag, "fragment k=%d: EINVAL, only base consumed",
+		k);
+      check_conv (tag, &r, -1, EINVAL, k, (const unsigned char *) "", 0);
+      /* Resend the complete code point (the retained fragment plus
+	 the remaining bytes).  */
+      memcpy (inbuf + 4, mark, 4);
+      r = call_raw (cd, "fragment completion", inbuf + 4, 4, 16);
+      snprintf (tag, sizeof tag, "fragment k=%d: completion combines", k);
+      check_conv (tag, &r, 0, 0, 0,
+		  (const unsigned char *) "\x0e\xec\xc3", 3);
+      TEST_COMPARE (iconv_close (cd), 0);
+    }
+
+  /* Flush capacity scan with the sb bit set: insufficient capacity
+     leaves the state unchanged.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "flush scan setup", L"\u00e6", 1, 16);
+    check_conv ("flush scan: setup", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    for (size_t cap = 0; cap < 4; cap++)
+      {
+	r = flush_cd (cd, "flush scan", cap);
+	char tag[64];
+	snprintf (tag, sizeof tag, "flush scan cap=%zu: E2BIG, no output",
+		  cap);
+	check_conv (tag, &r, -1, E2BIG, 0, (const unsigned char *) "", 0);
+      }
+    r = flush_cd (cd, "flush scan exact", 4);
+    check_conv ("flush scan cap=4: exact output", &r, 0, 0, 0,
+		(const unsigned char *) "\x0e\xd6\x7b\x0f", 4);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  /* Flush capacity scan with the db bit set.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "db flush setup", L"\u00e6\u0300", 2, 16);
+    check_conv ("db flush: establishes db mode", &r, 0, 0, 0,
+		(const unsigned char *) "\x0e\xec\xc3", 3);
+    r = call_wide (cd, "db flush pending", L"\u00e6", 1, 16);
+    check_conv ("db flush: pending base, no output", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    for (size_t cap = 0; cap < 3; cap++)
+      {
+	r = flush_cd (cd, "db flush scan", cap);
+	char tag[64];
+	snprintf (tag, sizeof tag, "db flush scan cap=%zu", cap);
+	check_conv (tag, &r, -1, E2BIG, 0, (const unsigned char *) "", 0);
+      }
+    r = flush_cd (cd, "db flush exact", 3);
+    check_conv ("db flush cap=3: no SO needed", &r, 0, 0, 0,
+		(const unsigned char *) "\xd6\x7b\x0f", 3);
+    r = flush_cd (cd, "db flush again", 16);
+    check_conv ("db flush: second flush is empty", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  /* Combination cut short after SO: nothing is consumed and the
+     retry does not write another SO.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "combine cap setup", L"\u00e6", 1, 16);
+    check_conv ("combine cap: setup", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    r = call_wide (cd, "combine cap 1", L"\u0300", 1, 1);
+    check_conv ("combine cap 1: SO only, E2BIG, mark unconsumed",
+		&r, -1, E2BIG, 4, (const unsigned char *) "\x0e", 1);
+    r = call_wide (cd, "combine cap retry", L"\u0300", 1, 2);
+    check_conv ("combine cap retry: combines without repeated SO",
+		&r, 0, 0, 0, (const unsigned char *) "\xec\xc3", 2);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+  /* The pending base character's own output cut short after SO:
+     the retry does not write another SO.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "base cap setup", L"\u304b", 1, 16);
+    check_conv ("base cap: setup", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    r = call_wide (cd, "base cap 1", L"a", 1, 1);
+    check_conv ("base cap 1: SO only, E2BIG, 'a' unconsumed",
+		&r, -1, E2BIG, 4, (const unsigned char *) "\x0e", 1);
+    unsigned char expect[] = { 0x44, 0x86, 0x0f, a_byte };
+    r = call_wide (cd, "base cap retry", L"a", 1, 16);
+    check_conv ("base cap retry: base then 'a', no repeated SO",
+		&r, 0, 0, 0, expect, 4);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  /* The discarding reset must not leak the pending character into
+     the next stream.  */
+  {
+    iconv_t cd = iconv_open (coding, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "reset setup", L"\u00e6", 1, 16);
+    check_conv ("reset: setup", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    TEST_COMPARE (iconv (cd, NULL, NULL, NULL, NULL), 0);
+    unsigned char expect[] = { a_byte };
+    r = call_wide (cd, "reset reuse", L"a", 1, 16);
+    check_conv ("reset: no pending leak, fresh state", &r, 0, 0, 0,
+		expect, 1);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  /* Multi-step chain: the base character is deferred across
+     iconv calls and flushed at the end.  */
+  {
+    iconv_t cd = iconv_open (coding, "UTF-8");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_raw (cd, "chain base", "\xc3\xa6", 2, 16);
+    check_conv ("chain: base deferred", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    r = call_raw (cd, "chain mark", "\xcc\x80", 2, 16);
+    check_conv ("chain: mark combines", &r, 0, 0, 0,
+		(const unsigned char *) "\x0e\xec\xc3", 3);
+    r = flush_cd (cd, "chain flush", 16);
+    check_conv ("chain: flush switches back", &r, 0, 0, 0,
+		(const unsigned char *) "\x0f", 1);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+  /* Multi-step chain with a lone base character: the flush writes
+     it out.  */
+  {
+    iconv_t cd = iconv_open (coding, "UTF-8");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_raw (cd, "chain lone base", "\xc3\xa6", 2, 16);
+    check_conv ("chain lone base: deferred", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    r = flush_cd (cd, "chain lone flush", 16);
+    check_conv ("chain lone base: flush writes it out", &r, 0, 0, 0,
+		(const unsigned char *) "\x0e\xd6\x7b\x0f", 4);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  /* With //IGNORE, an illegal character after a pending base
+     character is skipped.  */
+  {
+    char name[32];
+    snprintf (name, sizeof name, "%s//IGNORE", coding);
+    iconv_t cd = iconv_open (name, "WCHAR_T");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+    struct result r = call_wide (cd, "ignore setup", L"\u00e6", 1, 16);
+    check_conv ("ignore: setup", &r, 0, 0, 0,
+		(const unsigned char *) "", 0);
+    unsigned char bad[4];
+    cp_bytes (UINT32_MAX, bad);
+    r = call_raw (cd, "ignore illegal", bad, 4, 16);
+    check_conv ("ignore: pending out, illegal skipped", &r, 1, 0, 0,
+		(const unsigned char *) "\x0e\xd6\x7b", 3);
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+}
+
+static int
+do_test (void)
+{
+  test_pending ("IBM1390", 0x62);
+  test_pending ("IBM1399", 0x81);
+  return 0;
+}
+
+#include <support/test-driver.c>
-- 
2.43.0



More information about the Libc-alpha mailing list