[php-src] Issue #9008: `mb_detect_encoding()` sometimes not working with `null` as its second argument
| From: | machitgarha | Date: | Wed, 13 Jul 2022 22:30:29 +0000 |
| Subject: | [php-src] Issue #9008: `mb_detect_encoding()` sometimes not working with `null` as its second argument | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-241924@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/9008
Author: machitgarha
### Description
First of all, a special thanks to the progress made by all developers!
Calling
mb_detect_encoding() with the second argument being null may
return a really strange result, while changing it to mb_detect_order() fixes the issue.
Based on the official [PHP documentation](https://php.net/mb-detect-encoding), they must behave the
same.
I tried something like four hours, but cannot figure out what the problem exactly is. Even I cannot
create a *small* re-producable example; maybe the problem is environment- or context-dependent.
## Steps to Reproduce
1. Clone [Serenata](https://gitlab.com/Serenata/Serenata/) and navigate to its directory.
2. Run composer install.
3. Run ./vendor/bin/phpunit.
You should see there are lots of failures (i.e. 346). So what the hell? Wait, start editing the file
src/Analysis/SourceCodeReading/TextToUtf8Converter.php, go to line 15 and change it
from
```php
$encoding = mb_detect_encoding($code, null, true);
```
to
```php
$encoding = mb_detect_encoding($code, mb_detect_order(), true);
```
. Now re-run ./vendor/bin/phpunit. With this simple change, the issue is (almost) fixed
and only one failure remains.
I'm pretty sure that this is a new bug introduced in PHP 8.1. I compiled both PHP 8.0.19 and
8.1.8 the same way, with default configurations, and ran PHPUnit under both.
Under PHP 8.0, passing both values as the second argument works perfectly, and ALL tests pass. Under
PHP 8.1, I expect $encoding to hold either ASCII or UTF-8 (or
maybe even false), but it strangely equals to UUENCODE. Also, the one
remaining failed test after the change is also related to mb_detect_encoding(), and I
expect the same, but it randomly returns UUENCODE or UTF-7, causing the
+ signs of the string to disappear after mb_convert_encoding() (line 22 of
the file).
### PHP Version
PHP 8.1.7, PHP 8.1.8
### Operating System
Fedora Workstation 36