[php-src] Issue #25284: quoted_printable_encode splits UTF-8 characters (even though there is code to prevent that0
| From: | matthijskooijman | Date: | Sat, 07 Nov 2026 15:07:41 +0000 |
| Subject: | [php-src] Issue #25284: quoted_printable_encode splits UTF-8 characters (even though there is code to prevent that0 | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-254052@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/25284
Author: matthijskooijman
### Description
The following code:
```php
echo quoted_printable_encode("X" . str_repeat("\xc4\x85", 15));
```
Resulted in this output (breaking C485 over two lines):
```
x=C4=85=C4=85=C4=85=C4=85=C4=85=C4=85=C4=85=C4=85=C4=85=C4=85=C4=85=C4=
=85=C4=85=C4=85=C4=85
```
But I expected this output instead (keeping C485 together):
```
x=C4=85=C4=85=C4=85=C4=85=C4=85=C4=85=C4=85=C4=85=C4=85=C4=85=C4=85=
=C4==85=C4=85=C4=85=C4=85
```
When doing
quoted_printable_encode on multibyte strings, characters must not be split
over multiple lines. In commit c54d73d (14 years ago), code was added to prevent splitting utf-8
characters based on some hardcoded expected sizes for different utf-8 bytes, but that code seems to
no longer work.
This works by predicting how much bytes the UTF-8 sequence will be based on the value of the first
byte in the sequence, and then inserting a line break if there is not enough room for the entire
sequence.
The issue with the code seems to be that it works not only on the first byte in a utf-8 sequence,
but also incorrectly matches on followup bytes and thinking there will be one more =XX following
each of the followup bytes. For the last byte in a sequence that would normally fit exactly, this
would incorrectly insert a line break.
The mentioned commit also added a testcase, but that fails to catch this bug. It generates repeated
C485 utf-8 chars and after encoding 12 characters (12×=C4=85 == 72 bytes) it correctly
decides that adding just one more C4 is not allowed and splits the line. However, if we modify the
testcase to add an extra character at the start, thenafter encoding the "." and 12
characters at 73 output bytes, there is no room for the (incorrectly detected) extra output, and
there is an incorrect line break.
Relevant code is here: https://github.com/php/php-src/blob/a95ff6a621fb6912610bc573447e66e9b7237eb2/ext/standard/quot_print.c#L138-L182
UTF-8 encoding is as follows (from https://en.wikipedia.org/wiki/UTF-8):
<img width="662" height="209" alt="Image" src="https://github.com/user-attachments/assets/1d24ef01-4865-4241-bb62-dfdc52b7dc33"
/>
So bytes starting with 10 (0x80 - 0xBF) can only be in followup positions, so should not be
considered when checking for needed length (since the needed length should have been checked based
on the first byte already).
From looking at the code, I think the issue would be fixed by:
```diff
- if ((((lp+= 3) > PHP_QPRINT_MAXL) && (c <= 0x7f))
+ if ((((lp+= 3) > PHP_QPRINT_MAXL) && (c <= 0xbf))
- || ((c > 0x7f) && (c <= 0xdf) && ((lp + 3) > PHP_QPRINT_MAXL))
+ || ((c > 0xbf) && (c <= 0xdf) && ((lp + 3) > PHP_QPRINT_MAXL))
|| ((c > 0xdf) && (c <= 0xef) && ((lp + 6) > PHP_QPRINT_MAXL))
|| ((c > 0xef) && (c <= 0xf4) && ((lp + 9) > PHP_QPRINT_MAXL))) {
```
The valid character ranges are a bit more easy to see from this table (from https://www.unicode.org/versions/Unicode18.0.0/core-spec/chapter-3/#G27506):
<img width="638" height="383" alt="Image" src="https://github.com/user-attachments/assets/9c4cec23-0f68-4507-a29c-8b365b5d9086"
/>
There can be some variation: Bytes C0 and C1 are never valid (since those would produce characters
than can also be represented in a single byte, so the two-byte version is not allowed), but my above
suggestion does include these just in case. Alternative could be to count them as single byte, or
omit them from the checks, but then the (c <= 0x7f) check should also be removed
from the first line, to ensure that *all* characters are checked for having 3 bytes, and then some
bytes are additionally checked for extra room for multi-byte sequences.
Actuallly, this shows the code has another bug: Bytes > 0xf4 never triggere this if, so never
trigger a linebreak, which was also recently reported as #24441.
### PHP Version
```plain
PHP 8.5.9 (cli) (built: Aug 6 2026 02:40:54) (NTS)
Copyright (c) The PHP Group
Built by Ubuntu
Zend Engine v4.5.9, Copyright (c) Zend Technologies
with Zend OPcache v8.5.9, Copyright (c), by Zend Technologies
```
### Operating System
_No response_