]> granicus.if.org Git - php/commitdiff
CP5022{0,1,2}: treat truncated multibyte characters as error
authorAlex Dowad <alexinbeijing@gmail.com>
Wed, 13 Jan 2021 19:11:11 +0000 (21:11 +0200)
committerAlex Dowad <alexinbeijing@gmail.com>
Fri, 15 Jan 2021 19:55:41 +0000 (21:55 +0200)
ext/mbstring/libmbfl/filters/mbfilter_cp5022x.c
ext/mbstring/tests/cp5022x_encoding.phpt

index 7db315aa943a67b3999ab5b62e49acb1d201466c..7b4b2630f4f09e5774c7e3b7045140bbcb445a73 100644 (file)
@@ -39,6 +39,7 @@ typedef struct _mbfl_filt_conv_wchar_cp50220_ctx {
 static void mbfl_filt_conv_wchar_cp50220_ctor(mbfl_convert_filter *filt);
 static void mbfl_filt_conv_wchar_cp50220_dtor(mbfl_convert_filter *filt);
 static void mbfl_filt_conv_wchar_cp50220_copy(mbfl_convert_filter *src, mbfl_convert_filter *dest);
+static int mbfl_filt_conv_cp5022x_wchar_flush(mbfl_convert_filter *filter);
 
 const mbfl_encoding mbfl_encoding_jis_ms = {
        mbfl_no_encoding_jis_ms,
@@ -121,7 +122,7 @@ const struct mbfl_convert_vtbl vtbl_cp50220_wchar = {
        mbfl_filt_conv_common_ctor,
        NULL,
        mbfl_filt_conv_jis_ms_wchar,
-       mbfl_filt_conv_common_flush,
+       mbfl_filt_conv_cp5022x_wchar_flush,
        NULL,
 };
 
@@ -161,7 +162,7 @@ const struct mbfl_convert_vtbl vtbl_cp50221_wchar = {
        mbfl_filt_conv_common_ctor,
        NULL,
        mbfl_filt_conv_jis_ms_wchar,
-       mbfl_filt_conv_common_flush,
+       mbfl_filt_conv_cp5022x_wchar_flush,
        NULL,
 };
 
@@ -181,7 +182,7 @@ const struct mbfl_convert_vtbl vtbl_cp50222_wchar = {
        mbfl_filt_conv_common_ctor,
        NULL,
        mbfl_filt_conv_jis_ms_wchar,
-       mbfl_filt_conv_common_flush,
+       mbfl_filt_conv_cp5022x_wchar_flush,
        NULL,
 };
 
@@ -369,6 +370,15 @@ retry:
        return c;
 }
 
+static int mbfl_filt_conv_cp5022x_wchar_flush(mbfl_convert_filter *filter)
+{
+       if ((filter->status & 0xF) == 1) {
+               /* 2-byte (JIS X 0208 or 0212) character was truncated */
+               CK((*filter->output_function)(filter->cache | MBFL_WCSGROUP_THROUGH, filter->data));
+       }
+       return 0;
+}
+
 /*
  * wchar => JIS
  */
index 341faa1a430cc2ae89b9bbf0dbc1e2c96b726fc9..33a73a5e2e27abb89ded41c2ea2682dead59a6bb 100644 (file)
@@ -204,6 +204,13 @@ for ($i = 0x21; $i <= 0x7E; $i++) {
   }
 }
 
+/* Try truncated 2-byte characters */
+for ($i = 0x21; $i <= 0x7E; $i++) {
+  testInvalid("\x1B\$B" . chr($i), "\x00%", 'CP50220');
+  testInvalid("\x1B\$B" . chr($i), "\x00%", 'CP50221');
+  testInvalid("\x1B\$B" . chr($i), "\x00%", 'CP50222');
+}
+
 echo "CP932 support OK\n";
 
 /* Unicode codepoint for halfwidth katakana -> kuten code for ordinary katakana */