Skip to content

Commit 5fee30b

Browse files
committedJul 18, 2022
Fix new conversion filter for QPrint (same order of check as legacy code)
Because of checking for maximum line length *before* certain other checks, the new conversion filter for QPrint could produce different results from the old one in some cases. This was discovered while fuzzing the new implementation of mb_decode_numericentity.
1 parent 3cf4327 commit 5fee30b

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed
 

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

+14-9
Original file line numberDiff line numberDiff line change
@@ -318,24 +318,29 @@ static void mb_wchar_to_qprint(uint32_t *in, size_t len, mb_convert_buf *buf, bo
318318
* but raw bytes from 0x00-0xFF */
319319
uint32_t w = *in++;
320320

321-
/* QPrint actually mandates that line length should not be more than 76 characters,
322-
* but mbstring stops slightly short of that */
323-
if (chars_output >= 72) {
324-
MB_CONVERT_BUF_ENSURE(buf, out, limit, len + 4);
325-
out = mb_convert_buf_add3(out, '=', '\r', '\n');
326-
chars_output = 0;
327-
}
328-
329321
if (!w) {
330322
out = mb_convert_buf_add(out, '\0');
331323
chars_output = 0;
324+
continue;
332325
} else if (w == '\n') {
333326
MB_CONVERT_BUF_ENSURE(buf, out, limit, len + 2);
334327
out = mb_convert_buf_add2(out, '\r', '\n');
335328
chars_output = 0;
329+
continue;
336330
} else if (w == '\r') {
337331
/* No output */
338-
} else if (w >= 0x80 || w == '=') {
332+
continue;
333+
}
334+
335+
/* QPrint actually mandates that line length should not be more than 76 characters,
336+
* but mbstring stops slightly short of that */
337+
if (chars_output >= 72) {
338+
MB_CONVERT_BUF_ENSURE(buf, out, limit, len + 4);
339+
out = mb_convert_buf_add3(out, '=', '\r', '\n');
340+
chars_output = 0;
341+
}
342+
343+
if (w >= 0x80 || w == '=') {
339344
/* Not ASCII */
340345
MB_CONVERT_BUF_ENSURE(buf, out, limit, len + 3);
341346
out = mb_convert_buf_add3(out, '=', qprint_enc_nibble((w >> 4) & 0xF), qprint_enc_nibble(w & 0xF));

‎ext/mbstring/tests/qprint_encoding.phpt

+5
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ testConversion('あ', '=E3=81=82');
2323
testConversion("J'interdis aux marchands de vanter trop leurs marchandises. Car ils se font vite pédagogues et t'enseignent comme but ce qui n'est par essence qu'un moyen, et te trompant ainsi sur la route à suivre les voilà bientôt qui te dégradent, car si leur musique est vulgaire ils te fabriquent pour te la vendre une âme vulgaire.
2424
\xE2\x80\x89Antoine de Saint-Exupéry, Citadelle", "J'interdis aux marchands de vanter trop leurs marchandises. Car ils se f=\r\nont vite p=C3=A9dagogues et t'enseignent comme but ce qui n'est par esse=\r\nnce qu'un moyen, et te trompant ainsi sur la route =C3=A0 suivre les voi=\r\nl=C3=A0 bient=C3=B4t qui te d=C3=A9gradent, car si leur musique est vulg=\r\naire ils te fabriquent pour te la vendre une =C3=A2me vulgaire.\r\n=E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry, Citadelle");
2525

26+
// Regression test, found by a fuzzer
27+
testConversion("***,*-S,\xac,,\xb7,l,,,,\xb6UTF7,\xb5\xb5\xb5\xb5\xb5\xb5K\xb5\xb5!\xa4\xa4\xa4\xa4\xa4\xbd\xb5,\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5&", "***,*-S,=AC,,=B7,l,,,,=B6UTF7,=B5=B5=B5=B5=B5=B5K=B5=B5!=A4=A4=A4=A4=A4=BD=\r\n=B5,=B5=B5=B5=B5=B5=B5=B5=B5=B5&");
28+
2629
echo "Done!\n";
2730
?>
2831
--EXPECTF--
2932
Deprecated: mb_convert_encoding(): Handling QPrint via mbstring is deprecated; use quoted_printable_encode/quoted_printable_decode instead in %s
3033

3134
Deprecated: mb_convert_encoding(): Handling QPrint via mbstring is deprecated; use quoted_printable_encode/quoted_printable_decode instead in %s
3235

36+
Deprecated: mb_convert_encoding(): Handling QPrint via mbstring is deprecated; use quoted_printable_encode/quoted_printable_decode instead in %s
37+
3338
Deprecated: mb_convert_encoding(): Handling QPrint via mbstring is deprecated; use quoted_printable_encode/quoted_printable_decode instead in %s
3439
Done!

0 commit comments

Comments
 (0)
Please sign in to comment.