[php-src] Issue #8360: mbstring: UTF-8 sequences with out-of-bounds follow bytes are no longer substituted correctly
| From: | ausi | Date: | Thu, 14 Apr 2022 08:43:16 +0000 |
| Subject: | [php-src] Issue #8360: mbstring: UTF-8 sequences with out-of-bounds follow bytes are no longer substituted correctly | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-240946@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8360
Comment Author: ausi
> If we take \xDF\xC0 as an example, the byte \xDF is not illegal. But then \xC0 is. This is why
> I made it emit a single error marker.
Which totally makes sense ☺️
It took me quite some time to see how the specification ends up with two error markers here 🙈
> In general, we want
mbstring to comply as closely as possible
> with all relevant standards and to be compatible with as many legacy computing systems as possible.
❤️
> … I would like to ask if you have any other test cases which you can suggest should be added
> to the test suite.
These are the test cases I’ve written some time ago for an implementation of https://encoding.spec.whatwg.org/#utf-8-decoder
(every first string is the source, second is the target)
```php
["\x80", "\u{FFFD}"],
["\xFF", "\u{FFFD}"],
["\xC2\x7F", "\u{FFFD}\x7F"],
["\xC2\x80", "\xC2\x80"],
["\xDF\xBF", "\xDF\xBF"],
["\xDF\xC0", "\u{FFFD}\u{FFFD}"],
["\xE0\xA0\x7F", "\u{FFFD}\x7F"],
["\xE0\xA0\x80", "\xE0\xA0\x80"],
["\xEF\xBF\xBF", "\xEF\xBF\xBF"],
["\xEF\xBF\xC0", "\u{FFFD}\u{FFFD}"],
["\xF0\x90\x80\x7F", "\u{FFFD}\x7F"],
["\xF0\x90\x80\x80", "\xF0\x90\x80\x80"],
["\xF4\x8F\xBF\xBF", "\xF4\x8F\xBF\xBF"],
["\xF4\x8F\xBF\xC0", "\u{FFFD}\u{FFFD}"],
["\xFA\x80\x80\x80\x80", "\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}"],
["\xFB\xBF\xBF\xBF\xBF", "\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}"],
["\xFD\x80\x80\x80\x80\x80",
"\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}"],
["\xFD\xBF\xBF\xBF\xBF\xBF",
"\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}"],
```
```php
return array_map(function($i) {
return [chr($i), $i < 128 ? chr($i) : "\u{FFFD}"];
}, range(0, 255));
```
There might be some test cases to borrow from libicu or the
web-platform-tests but I couldn’t find them just now.