Skip to content

Commit

Permalink
bug #13527 [Validator] drop grapheme_strlen in LengthValidator (nicol…
Browse files Browse the repository at this point in the history
…as-grekas)

This PR was merged into the 2.3 branch.

Discussion
----------

[Validator] drop grapheme_strlen in LengthValidator

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #13491
| License       | MIT
| Doc PR        | -

As stated in #13491, validating the length of a string with grapheme_strlen is not suited to validating input because a very long string can in fact have a length of 1 when counted with grapheme_strlen.
Counting UTF-8 characters (not clusters) is not subject to this problem.
The attached patch removes using grapheme_strlen but also adds more fallback when couting the length of strings, using iconv (which is more broadly avail. than mbstring) or PCRE for UTF-8 strings.

Commits
-------

915fcd8 [Validator] drop grapheme_strlen in LengthValidator
  • Loading branch information
fabpot committed Jan 26, 2015
2 parents 975b8a8 + 915fcd8 commit faaa4fe
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Symfony/Component/Validator/Constraints/LengthValidator.php
Expand Up @@ -35,12 +35,20 @@ public function validate($value, Constraint $constraint)

$stringValue = (string) $value;

if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
$length = grapheme_strlen($stringValue);
if ('UTF8' === $charset = strtoupper($constraint->charset)) {
$charset = 'UTF-8';
}

if (function_exists('iconv_strlen')) {
$length = iconv_strlen($stringValue, $constraint->charset);
} elseif (function_exists('mb_strlen')) {
$length = mb_strlen($stringValue, $constraint->charset);
} else {
} elseif ('UTF-8' !== $charset) {
$length = strlen($stringValue);
} elseif (function_exists('utf8_decode')) {
$length = strlen(utf8_decode($stringValue));
} else {
preg_replace('/./u', '', $stringValue, -1, $length);
}

if ($constraint->min == $constraint->max && $length != $constraint->min) {
Expand Down

0 comments on commit faaa4fe

Please sign in to comment.