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