php-src icon indicating copy to clipboard operation
php-src copied to clipboard

quoted_printable_encode() produces lines longer than 76 characters for bytes 0xF5 to 0xFF

Open sebxt opened this issue 1 month ago • 1 comments

Description

quoted_printable_encode() is documented to follow RFC 2045 section 6.7, which limits encoded lines to 76 characters. Input bytes in the range 0xF5 to 0xFF are encoded as =XX but never trigger a soft line break, so a run of them ends up on a single line of any length.

The following code:

<?php
$encoded = quoted_printable_encode(str_repeat("\xFF", 30));
var_dump(array_map('strlen', explode("\r\n", $encoded)));

Resulted in this output:

array(1) {
  [0]=>
  int(90)
}

But I expected this output instead:

array(2) {
  [0]=>
  int(76)
  [1]=>
  int(15)
}

The same happens with any binary data that contains such bytes, for example a random string of a few hundred bytes regularly produces 78 or 79 character lines. The convert.quoted-printable-encode stream filter is not affected.

Root cause: in php_quot_print_encode() (ext/standard/quot_print.c) the soft break condition has one branch for bytes up to 0x7F and three branches for UTF-8 lead and continuation bytes (0x80 to 0xDF, 0xE0 to 0xEF, 0xF0 to 0xF4). Bytes 0xF5 to 0xFF match none of them, so the line position keeps growing without a break being inserted.

PHP Version

PHP 8.7.0-dev (master at b7256eee), also present in all supported branches

Operating System

Any

sebxt avatar Oct 19 '26 15:10 sebxt

@sebxt I also noticed another problem with quoted_printable_encode, with a suggested fix at #25284. Maybe you would like to include a fix in your PR, since I do not have a PHP dev setup ready to submit the fix.

Also, it is quite crazy that within a few weeks of each other we independently find two bugs with some 14 year old code... :-)

matthijskooijman avatar Nov 07 '26 15:11 matthijskooijman