Skip to content

Commit 888f5d7

Browse files
committed
CP5022{0,1,2}: treat truncated multibyte characters as error
1 parent 2a93a8b commit 888f5d7

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

ext/mbstring/libmbfl/filters/mbfilter_cp5022x.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef struct _mbfl_filt_conv_wchar_cp50220_ctx {
3939
static void mbfl_filt_conv_wchar_cp50220_ctor(mbfl_convert_filter *filt);
4040
static void mbfl_filt_conv_wchar_cp50220_dtor(mbfl_convert_filter *filt);
4141
static void mbfl_filt_conv_wchar_cp50220_copy(mbfl_convert_filter *src, mbfl_convert_filter *dest);
42+
static int mbfl_filt_conv_cp5022x_wchar_flush(mbfl_convert_filter *filter);
4243

4344
const mbfl_encoding mbfl_encoding_jis_ms = {
4445
mbfl_no_encoding_jis_ms,
@@ -121,7 +122,7 @@ const struct mbfl_convert_vtbl vtbl_cp50220_wchar = {
121122
mbfl_filt_conv_common_ctor,
122123
NULL,
123124
mbfl_filt_conv_jis_ms_wchar,
124-
mbfl_filt_conv_common_flush,
125+
mbfl_filt_conv_cp5022x_wchar_flush,
125126
NULL,
126127
};
127128

@@ -161,7 +162,7 @@ const struct mbfl_convert_vtbl vtbl_cp50221_wchar = {
161162
mbfl_filt_conv_common_ctor,
162163
NULL,
163164
mbfl_filt_conv_jis_ms_wchar,
164-
mbfl_filt_conv_common_flush,
165+
mbfl_filt_conv_cp5022x_wchar_flush,
165166
NULL,
166167
};
167168

@@ -181,7 +182,7 @@ const struct mbfl_convert_vtbl vtbl_cp50222_wchar = {
181182
mbfl_filt_conv_common_ctor,
182183
NULL,
183184
mbfl_filt_conv_jis_ms_wchar,
184-
mbfl_filt_conv_common_flush,
185+
mbfl_filt_conv_cp5022x_wchar_flush,
185186
NULL,
186187
};
187188

@@ -369,6 +370,15 @@ mbfl_filt_conv_jis_ms_wchar(int c, mbfl_convert_filter *filter)
369370
return c;
370371
}
371372

373+
static int mbfl_filt_conv_cp5022x_wchar_flush(mbfl_convert_filter *filter)
374+
{
375+
if ((filter->status & 0xF) == 1) {
376+
/* 2-byte (JIS X 0208 or 0212) character was truncated */
377+
CK((*filter->output_function)(filter->cache | MBFL_WCSGROUP_THROUGH, filter->data));
378+
}
379+
return 0;
380+
}
381+
372382
/*
373383
* wchar => JIS
374384
*/

ext/mbstring/tests/cp5022x_encoding.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ for ($i = 0x21; $i <= 0x7E; $i++) {
204204
}
205205
}
206206

207+
/* Try truncated 2-byte characters */
208+
for ($i = 0x21; $i <= 0x7E; $i++) {
209+
testInvalid("\x1B\$B" . chr($i), "\x00%", 'CP50220');
210+
testInvalid("\x1B\$B" . chr($i), "\x00%", 'CP50221');
211+
testInvalid("\x1B\$B" . chr($i), "\x00%", 'CP50222');
212+
}
213+
207214
echo "CP932 support OK\n";
208215

209216
/* Unicode codepoint for halfwidth katakana -> kuten code for ordinary katakana */

0 commit comments

Comments
 (0)