Skip to content

[Form] fix parsing invalid floating point numbers #24460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2017
Merged

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Oct 6, 2017

Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #19854, #22586
License MIT
Doc PR

Should make AppVeyor builds pass again. Code borrowed from NumberToLocalizedStringTransformer.

}

return $value;
if (function_exists('mb_detect_encoding') && false !== $encoding = mb_detect_encoding($value, null, true)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function_exists should be fully qualified to benefit from PHP 7 compile-time evaluation

}

return $value;
if (function_exists('mb_detect_encoding') && false !== $encoding = mb_detect_encoding($value, null, true)) {
$length = mb_strlen($value, $encoding);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about it ? NumberFormatter::parse is documented as setting the offset in the string on php.net. This offset will be based on bytes, not on characters, so you should not be using mb_strlen

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I just adapted the code from the NumberToLocalizedStringTransformer. Seems this was done on purpose in #7902.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the tests for the NumberToLocalizedStringTransformer and copied over some of them. They prove that we indeed need to use the mbstring extension (for example, the exception message in testReverseTransformDisallowsCenteredExtraCharactersMultibyte() would not match otherwise).

$remainder = trim($remainder, " \t\n\r\0\x0b\xc2\xa0");

if ('' !== $remainder) {
throw new TransformationFailedException(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need tests reaching this new exception

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PercentToLocalizedStringTransformerTest ::testDecimalSeparatorMayNotBeDotIfGroupingSeparatorIsDot() is failing without this change on the 3.4 branch on AppVeyor: https://ci.appveyor.com/project/fabpot/symfony/build/1.0.28085#L1537

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be necessary to replace \Locale::setDefault('de_AT'); by \Locale::setDefault('de_DE'); at

as for

See also aa3c66e

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also comment #19854 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakzal Why was the change in aa3c66e necessary?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you see the comment #19854 (comment) ? You have maybe not see the previous link

ICU data was indeed changed for de_AT. It is taken from cldr though, which also has this bit
changed. So it looks like it's a bug in CLDR (unless Austria really got rid of the period as a >grouping separator).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaa2000 Indeed, changing the locale to de_DE fixes the initial issue.

@xabbuh
Copy link
Member Author

xabbuh commented Oct 7, 2017

Status: Needs Review


if ('' !== $remainder) {
throw new TransformationFailedException(
sprintf('The number contains unrecognized characters: "%s"', $remainder)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exception should be on one line

$length = mb_strlen($value, $encoding);
$remainder = mb_substr($value, $position, $length, $encoding);
} else {
$length = strlen($value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be \strlen(PHP-CS-Fixer/PHP-CS-Fixer#3048)

@fabpot
Copy link
Member

fabpot commented Oct 10, 2017

Thank you @xabbuh.

@fabpot fabpot merged commit 042eac4 into symfony:2.7 Oct 10, 2017
fabpot added a commit that referenced this pull request Oct 10, 2017
This PR was merged into the 2.7 branch.

Discussion
----------

[Form] fix parsing invalid floating point numbers

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19854, #22586
| License       | MIT
| Doc PR        |

Should make AppVeyor builds pass again. Code borrowed from `NumberToLocalizedStringTransformer`.

Commits
-------

042eac4 [Form] fix parsing invalid floating point numbers
@xabbuh xabbuh deleted the pr-22586 branch October 10, 2017 06:04
This was referenced Nov 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants