diff --git a/NEWS b/NEWS index 63a7da1355f6..306ece22fed4 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,11 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? ????, PHP 8.4.30 +- Standard: + . Fixed bug GH-25956 (quoted-printable stream filter leaves trailing + whitespace unencoded with custom line breaks). (Sebastian Turner, + Ilia Alshanetsky) + 17 Dec 2026, PHP 8.4.29 diff --git a/ext/standard/filters.c b/ext/standard/filters.c index 3c6cfd84af06..765dfab6fd86 100644 --- a/ext/standard/filters.c +++ b/ext/standard/filters.c @@ -650,6 +650,7 @@ static php_conv_err_t php_conv_qprint_encode_convert(php_conv_qprint_encode *ins unsigned int lb_cnt; unsigned int trail_ws; int opts; + bool flush; static const char qp_digits[] = "0123456789ABCDEF"; line_ccnt = inst->line_ccnt; @@ -657,12 +658,10 @@ static php_conv_err_t php_conv_qprint_encode_convert(php_conv_qprint_encode *ins lb_ptr = inst->lb_ptr; lb_cnt = inst->lb_cnt; - if (in_pp == NULL || in_left_p == NULL) { - return PHP_CONV_ERR_SUCCESS; - } + flush = (in_pp == NULL || in_left_p == NULL); - ps = (unsigned char *)(*in_pp); - icnt = *in_left_p; + ps = flush ? NULL : (unsigned char *)(*in_pp); + icnt = flush ? 0 : *in_left_p; pd = (unsigned char *)(*out_pp); ocnt = *out_left_p; trail_ws = 0; @@ -696,7 +695,7 @@ static php_conv_err_t php_conv_qprint_encode_convert(php_conv_qprint_encode *ins } } - if (lb_ptr >= lb_cnt && icnt == 0) { + if (icnt == 0 && (lb_ptr >= lb_cnt || !flush)) { break; } @@ -734,7 +733,7 @@ static php_conv_err_t php_conv_qprint_encode_convert(php_conv_qprint_encode *ins ps2 = ps; trail_ws = 1; - for (j = icnt - 1; j > 0; j--, ps2++) { + for (j = icnt; j > 1; j--, ps2++) { if (*ps2 == inst->lbchars[lb_cnt2]) { lb_cnt2++; if (lb_cnt2 >= inst->lbchars_len) { @@ -813,8 +812,10 @@ static php_conv_err_t php_conv_qprint_encode_convert(php_conv_qprint_encode *ins } } - *in_pp = (const char *)ps; - *in_left_p = icnt; + if (!flush) { + *in_pp = (const char *)ps; + *in_left_p = icnt; + } *out_pp = (char *)pd; *out_left_p = ocnt; inst->line_ccnt = line_ccnt; diff --git a/ext/standard/tests/filters/gh25956.phpt b/ext/standard/tests/filters/gh25956.phpt new file mode 100644 index 000000000000..0eb0cb9b12a9 --- /dev/null +++ b/ext/standard/tests/filters/gh25956.phpt @@ -0,0 +1,43 @@ +--TEST-- +GH-25956 quoted-printable encode preserves trailing whitespace with a partial line break +--FILE-- + $lineBreak, + 'line-length' => 76, + ]); + $encoded = stream_get_contents($stream); + fclose($stream); + return bin2hex($encoded); +} + +function encodeWrite(string $input, string $lineBreak): string { + $stream = fopen('php://temp', 'w+'); + $filter = stream_filter_append($stream, 'convert.quoted-printable-encode', STREAM_FILTER_WRITE, [ + 'line-break-chars' => $lineBreak, + 'line-length' => 76, + ]); + fwrite($stream, $input); + stream_filter_remove($filter); + rewind($stream); + $encoded = stream_get_contents($stream); + fclose($stream); + return bin2hex($encoded); +} + +echo "read trailing space: ", encodeRead("abc ", " X"), "\n"; +echo "read trailing tab: ", encodeRead("abc\t", "\tX"), "\n"; +echo "read two pending spaces: ", encodeRead("abc ", " X"), "\n"; +echo "read complete line break: ", encodeRead("abc Xdef", " X"), "\n"; +echo "write trailing space: ", encodeWrite("abc ", " X"), "\n"; +?> +--EXPECT-- +read trailing space: 6162633d3230 +read trailing tab: 6162633d3039 +read two pending spaces: 6162633d32303d3230 +read complete line break: 6162632058646566 +write trailing space: 6162633d3230 diff --git a/ext/standard/tests/filters/qp_encode_partial_line_break.phpt b/ext/standard/tests/filters/qp_encode_partial_line_break.phpt new file mode 100644 index 000000000000..5df519f72a76 --- /dev/null +++ b/ext/standard/tests/filters/qp_encode_partial_line_break.phpt @@ -0,0 +1,35 @@ +--TEST-- +convert.quoted-printable-encode keeps a partial line break sequence until more input or the end of the stream +--FILE-- + 76, + 'line-break-chars' => "\r\n", + ]); + foreach ($chunks as $chunk) { + fwrite($fp, $chunk); + } + stream_filter_remove($filter); + rewind($fp); + return stream_get_contents($fp); +} + +foreach ([["a\r"], ["a", "=\r"], ["Hello=\r", "\nWorld"], ["Hello=\r\n"], ["=\r\n\r", "\n"]] as $chunks) { + $encoded = encode($chunks); + echo json_encode($encoded), "\n"; + var_dump(quoted_printable_decode($encoded) === implode('', $chunks)); +} +?> +--EXPECT-- +"a=0D" +bool(true) +"a=3D=0D" +bool(true) +"Hello=3D\r\nWorld" +bool(true) +"Hello=3D\r\n" +bool(true) +"=3D\r\n\r\n" +bool(true)