Skip to content

Commit f3c8efd

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.
1 parent 18e526c commit f3c8efd

28 files changed

+35
-2
lines changed

‎ext/mbstring/libmbfl/filters/mbfilter_big5.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ static int mbfl_filt_conv_big5_wchar_flush(mbfl_convert_filter *filter)
257257
{
258258
if (filter->status == 1) {
259259
/* 2-byte character was truncated */
260+
filter->status = 0;
260261
CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
261262
}
262263

‎ext/mbstring/libmbfl/filters/mbfilter_cp5022x.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ static int mbfl_filt_conv_cp5022x_wchar_flush(mbfl_convert_filter *filter)
322322
* escape sequence was truncated */
323323
CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
324324
}
325+
filter->status = 0;
325326

326327
if (filter->flush_function) {
327328
(*filter->flush_function)(filter->data);
@@ -824,7 +825,7 @@ static int mbfl_filt_conv_wchar_cp50222_flush(mbfl_convert_filter *filter)
824825
CK((*filter->output_function)(0x28, filter->data)); /* '(' */
825826
CK((*filter->output_function)(0x42, filter->data)); /* 'B' */
826827
}
827-
filter->status &= 0xff;
828+
filter->status = 0;
828829

829830
if (filter->flush_function) {
830831
(*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
@@ -178,6 +178,7 @@ static int mbfl_filt_conv_cp51932_wchar_flush(mbfl_convert_filter *filter)
178178
if (filter->status) {
179179
/* Input string was truncated */
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_cp932.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ static int mbfl_filt_conv_cp932_wchar_flush(mbfl_convert_filter *filter)
217217
{
218218
if (filter->status) {
219219
(*filter->output_function)(MBFL_BAD_INPUT, filter->data);
220+
filter->status = 0;
220221
}
221222

222223
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
@@ -166,6 +166,7 @@ static int mbfl_filt_conv_cp936_wchar_flush(mbfl_convert_filter *filter)
166166
{
167167
if (filter->status) {
168168
/* 2-byte character was truncated */
169+
filter->status = 0;
169170
CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
170171
}
171172

‎ext/mbstring/libmbfl/filters/mbfilter_euc_cn.c‎

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

‎ext/mbstring/libmbfl/filters/mbfilter_euc_jp.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ static int mbfl_filt_conv_eucjp_wchar_flush(mbfl_convert_filter *filter)
180180
{
181181
if (filter->status) {
182182
(*filter->output_function)(MBFL_BAD_INPUT, filter->data);
183+
filter->status = 0;
183184
}
184185

185186
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
@@ -226,6 +226,7 @@ static int mbfl_filt_conv_eucjpwin_wchar_flush(mbfl_convert_filter *filter)
226226
{
227227
if (filter->status) {
228228
(*filter->output_function)(MBFL_BAD_INPUT, filter->data);
229+
filter->status = 0;
229230
}
230231

231232
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
@@ -193,6 +193,7 @@ static int mbfl_filt_conv_euckr_wchar_flush(mbfl_convert_filter *filter)
193193
{
194194
if (filter->status == 1) {
195195
/* 2-byte character was truncated */
196+
filter->status = 0;
196197
CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
197198
}
198199

‎ext/mbstring/libmbfl/filters/mbfilter_euc_tw.c‎

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

0 commit comments

Comments
 (0)