10000 [Validator] `Type` constraint considers `NAN` and `INF` as floats · Issue #50782 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Type constraint considers NAN and INF as floats #50782

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

Closed
aprat84 opened this issue Jun 26, 2023 · 4 comments · Fixed by #50907
Closed

[Validator] Type constraint considers NAN and INF as floats #50782

aprat84 opened this issue Jun 26, 2023 · 4 comments · Fixed by #50907

Comments

@aprat84
Copy link
Contributor
aprat84 commented Jun 26, 2023

Symfony version(s) affected

6.3

Description

If a number is outside of 2.2250738585072014E-308 to 1.7976931348623158E+308 range, PHP converts it to -INF or INF.

Values INF, -INF and NAN are considered float in PHP as for the is_float function.

The Type constraint relies in this function to check for a valid float.

Now, are this valid float numbers? IMHO no.

FTR: this doesn't happen with integers, as PHP converts them to float once they go outside the PHP_INT_MIN and PHP_INT_MAX range...

How to reproduce

var_dump(is_float(INF));
var_dump(is_float(NAN));

Possible Solution

Add an extra check for is_finite, when dealing with floats?

Additional Context

No response

@aprat84 aprat84 added the Bug label Jun 26, 2023
@aprat84 aprat84 changed the title [Validator] Type constraint allows NAN and INF as floats [Validator] Type constraint considers NAN and INF as floats Jun 26, 2023
@xabbuh
Copy link
Member
xabbuh commented Jun 27, 2023

@aprat84 Sounds reasonable to me, can you open a PR with your suggestion (against the 5.4 branch)?

@aprat84
Copy link
Contributor Author
aprat84 commented Jul 3, 2023

Maybe the best solution would be to add a new constraint Finite (and its FiniteValidator), so we can combine it with Type constraint:

#[Assert\Type(['int', 'float'])]
#[Assert\Finite]

Instead of patching the Type constraint adding range validation...

@guillaume-a
Copy link
Contributor

Hello, I made an attemps to create Finite validator. Hope that helps.

@magikid
Copy link
Contributor
magikid commented Jul 22, 2023

Both NaN and Inf are valid floats as defined in IEEE 754. It would be very strange to treat them as invalid values.

@fabpot fabpot closed this as completed in f404704 Jul 30, 2023
OskarStark added a commit to symfony/symfony-docs that referenced this issue Jul 31, 2023
…inite-float` and `finite-number` validations (guillaume-a)

This PR was merged into the 6.4 branch.

Discussion
----------

[Validator] Update `Type` constraint, add `number`, `finite-float` and `finite-number` validations

Documentation for PR
* symfony/symfony#50907

Related to
* symfony/symfony#50782

This is my first sf-docs PR, please tell me if I did anything wrong.
Thank you.

Commits
-------

458fdd5 [Validator] Update `Type` constraint, add `number`, `finite-float` and `finite-number` validations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
0