1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-09 16:32:39 +01:00

Fix PHP 8.0 ValueError calling mb_convert_encoding() with an invalid encoding

Summary:
Per https://www.php.net/manual/en/function.mb-convert-encoding.php, as of PHP 8.0.0, a ValueError is thrown if the value of `to_encoding` or `from_encoding` is an invalid encoding but a ValueError is not suppressed by the stfu operator ("@").

Origin of the function:

https://secure.phabricator.com/rPHU72ad8fd0f05b0d84f7d8efd7db62ad0b3ba4431f

Premising that Arcanist elevates warnings to exception, now we just try-catch.

Closes T15423

Test Plan:
On `/diffusion/edit/1/page/encoding/`,
* enter a valid encoding, such as "7bit", successfully changed encoding
* enter a valid encoding with random capitalization, such as "7biT", successfully changed encoding
* enter a valid alias encoding, such as "ISO-10646-UCS-2", successfully changed encoding
* enter a valid alias encoding with random capitalization, such as "isO-10646-uCS-2", successfully changed encoding
* enter an invalid encoding, such as "whatever", get error message "Repository encoding "whatever" is not valid: String conversion from encoding 'UTF-8' to encoding 'whatever' failed: mb_convert_encoding(): Argument #2 ($to_encoding) must be a valid encoding, "whatever" given"
In any case, no exception is shown anymore.

Reviewers: O1 Blessed Committers, valerio.bozzolan, speck

Reviewed By: O1 Blessed Committers, valerio.bozzolan, speck

Subscribers: 0, speck, tobiaswiese, valerio.bozzolan, Matthew, Cigaryno

Maniphest Tasks: T15423

Differential Revision: https://we.phorge.it/D25249
This commit is contained in:
Andre Klapper 2023-07-29 12:20:14 +03:00
parent 6c6f47bf9c
commit 788098096e

View file

@ -718,13 +718,10 @@ function phutil_utf8_convert($string, $to_encoding, $from_encoding) {
'mbstring'));
}
$result = @mb_convert_encoding($string, $to_encoding, $from_encoding);
if ($result === false) {
$message = error_get_last();
if ($message) {
$message = idx($message, 'message', pht('Unknown error.'));
}
try {
$result = mb_convert_encoding($string, $to_encoding, $from_encoding);
} catch (Throwable $ex) {
$message = $ex->getMessage();
throw new Exception(
pht(
"String conversion from encoding '%s' to encoding '%s' failed: %s",