Skip to content

Commit 5812b4f

Browse files
committed
In legacy text conversion filters, reset filter state in 'flush' function
Up until now, I believed that mbstring had been designed such that (legacy) text conversion filter objects should not be re-used after the 'flush' function is called to complete a text conversion operation. However, it turns out that the implementation of _php_mb_encoding_handler_ex DID re-use filter objects after flush. That means that functions which were based on _php_mb_encoding_handler_ex, including mb_parse_str and php_mb_post_handler, would break in some cases; state left over from converting one substring (perhaps a variable name) would affect the results of converting another substring (perhaps the value of the same variable), and could cause extraneous characters to get inserted into the output. All this code should be deleted soon, but fixing it helps me to avoid spurious failures when fuzzing the new/old code to look for differences in behavior. (This bug fix commit was originally applied to PHP-8.2 when fuzzing the new mbstring text conversion code to check for differences with the old code. Later, Kentaro Ohkouchi kindly reported a problem with mb_encode_mimeheader under PHP 8.1 which was caused by the same issue. Hence, this commit was backported to PHP-8.1.) Fixes GH-9683.
1 parent be53e5e commit 5812b4f

28 files changed

+45
-2
lines changed

ext/mbstring/libmbfl/filters/mbfilter_big5.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ static int mbfl_filt_conv_big5_wchar_flush(mbfl_convert_filter *filter)
251251
{
252252
if (filter->status == 1) {
253253
/* 2-byte character was truncated */
254+
filter->status = 0;
254255
CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
255256
}
256257

ext/mbstring/libmbfl/filters/mbfilter_cp5022x.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ static int mbfl_filt_conv_cp5022x_wchar_flush(mbfl_convert_filter *filter)
312312
/* 2-byte (JIS X 0208 or 0212) character was truncated */
313313
CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
314314
}
315+
filter->status = 0;
315316

316317
if (filter->flush_function) {
317318
(*filter->flush_function)(filter->data);
@@ -650,7 +651,7 @@ mbfl_filt_conv_wchar_cp50222_flush(mbfl_convert_filter *filter)
650651
CK((*filter->output_function)(0x28, filter->data)); /* '(' */
651652
CK((*filter->output_function)(0x42, filter->data)); /* 'B' */
652653
}
653-
filter->status &= 0xff;
654+
filter->status = 0;
654655

655656
if (filter->flush_function) {
656657
(*filter->flush_function)(filter->data);

ext/mbstring/libmbfl/filters/mbfilter_cp51932.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ static int mbfl_filt_conv_cp51932_wchar_flush(mbfl_convert_filter *filter)
176176
if (filter->status) {
177177
/* Input string was truncated */
178178
(*filter->output_function)(MBFL_BAD_INPUT, filter->data);
179+
filter->status = 0;
179180
}
180181

181182
if (filter->flush_function) {

ext/mbstring/libmbfl/filters/mbfilter_cp932.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ static int mbfl_filt_conv_cp932_wchar_flush(mbfl_convert_filter *filter)
272272
{
273273
if (filter->status) {
274274
(*filter->output_function)(MBFL_BAD_INPUT, filter->data);
275+
filter->status = 0;
275276
}
276277

277278
if (filter->flush_function) {

ext/mbstring/libmbfl/filters/mbfilter_cp936.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ static int mbfl_filt_conv_cp936_wchar_flush(mbfl_convert_filter *filter)
169169
{
170170
if (filter->status) {
171171
/* 2-byte character was truncated */
172+
filter->status = 0;
172173
CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
173174
}
174175

ext/mbstring/libmbfl/filters/mbfilter_euc_cn.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ static int mbfl_filt_conv_euccn_wchar_flush(mbfl_convert_filter *filter)
210210
{
211211
if (filter->status == 1) {
212212
/* 2-byte character was truncated */
213+
filter->status = 0;
213214
CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
214215
}
215216

ext/mbstring/libmbfl/filters/mbfilter_euc_jp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ static int mbfl_filt_conv_eucjp_wchar_flush(mbfl_convert_filter *filter)
178178
{
179179
if (filter->status) {
180180
(*filter->output_function)(MBFL_BAD_INPUT, filter->data);
181+
filter->status = 0;
181182
}
182183

183184
if (filter->flush_function) {

ext/mbstring/libmbfl/filters/mbfilter_euc_jp_win.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ static int mbfl_filt_conv_eucjpwin_wchar_flush(mbfl_convert_filter *filter)
223223
{
224224
if (filter->status) {
225225
(*filter->output_function)(MBFL_BAD_INPUT, filter->data);
226+
filter->status = 0;
226227
}
227228

228229
if (filter->flush_function) {

ext/mbstring/libmbfl/filters/mbfilter_euc_kr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ static int mbfl_filt_conv_euckr_wchar_flush(mbfl_convert_filter *filter)
197197
{
198198
if (filter->status == 1) {
199199
/* 2-byte character was truncated */
200+
filter->status = 0;
200201
CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
201202
}
202203

ext/mbstring/libmbfl/filters/mbfilter_euc_tw.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ static int mbfl_filt_conv_euctw_wchar_flush(mbfl_convert_filter *filter)
243243
{
244244
if (filter->status) {
245245
/* 2-byte or 4-byte character was truncated */
246+
filter->status = 0;
246247
CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
247248
}
248249

0 commit comments

Comments
 (0)