Skip to content

Commit

Permalink
bug #40162 [Intl] fix Locale::getFallback() throwing exception on lon…
Browse files Browse the repository at this point in the history
…g $locale (AmirHo3ein13)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[Intl] fix Locale::getFallback() throwing exception on long $locale

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #39100
| License       | MIT
| Doc PR        |

`Locale::getFallback()` throws an exception when the `$locale` length is greater than `INTL_MAX_LOCALE_LEN` so I added a condition to check if locale_parse return null, the `Locale::getFallback()` don't call `\count` function and just return null instead.

Commits
-------

a89ced8 [Intl] fix Locale::getFallback() throwing exception on long $locale
  • Loading branch information
nicolas-grekas committed Feb 17, 2021
2 parents 1a5aec1 + a89ced8 commit 9765b5a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Symfony/Component/Intl/Locale.php
Expand Up @@ -68,7 +68,8 @@ public static function getDefaultFallback(): ?string
public static function getFallback(string $locale): ?string
{
if (\function_exists('locale_parse')) {
$localeSubTags = locale_parse($locale);
$localeSubTags = locale_parse($locale) ?? ['language' => $locale];

if (1 === \count($localeSubTags)) {
if ('root' !== self::$defaultFallback && self::$defaultFallback === $localeSubTags['language']) {
return 'root';
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Intl/Tests/LocaleTest.php
Expand Up @@ -70,4 +70,16 @@ public function testDefaultRootFallback()

Locale::setDefaultFallback($prev);
}

/**
* @requires function locale_parse
*/
public function testLongLocaleFallback()
{
$locale = 'LC_TYPE=fr_FR.UTF-8;LC_NUMERIC=C;LC_TIME=fr_FR.UTF-8;LC_COLLATE=fr_FR.UTF-8;'.
'LC_MONETARY=fr_FR.UTF-8;LC_MESSAGES=fr_FR.UTF-8;LC_PAPER=fr_FR.UTF-8;LC_NAME=fr_FR.UTF-8;'.
'LC_ADDRESS=fr_FR.UTF-8;LC_TELEPHONE=fr_FR.UTF-8;LC_MEASUREMENT=fr_FR.UTF-8;LC_IDENTIFICATION=fr_FR.UTF-8';

$this->assertNull(Locale::getFallback($locale));
}
}

0 comments on commit 9765b5a

Please sign in to comment.