Literal: Check Literal::Integer at limits and when overflowing#38
Literal: Check Literal::Integer at limits and when overflowing#38Hywan merged 7 commits intotagua-vm:masterfrom
Literal::Integer at limits and when overflowing#38Conversation
The specification says: > This type is binary, signed, and uses twos-complement representation > for negative values. The range of values that can be stored is > implementation-defined; however, the range [-2147483648, 2147483647], > must be supported. This range must be finite. https://github.com/php/php-langspec/blob/master/spec/05-types.md#the-integer-type
|
I suspect that overflowing hex literals are more common than one might think due to people trying to use unsigned 64-bit literals, like |
|
@nikic I am pretty sure it is. Even in Rust, the |
source/rules/literals.rs
Outdated
| #[test] | ||
| fn case_octal_maximum_integer_value() { | ||
| let input = b"0777777777777777777777"; | ||
| let output = Done(&b""[..], Literal::Integer(!(1i64 << 63))); |
There was a problem hiding this comment.
We can use std::i64::MAX here.
It is better to use the `MAX` constant instead of computing the maximum value with bitwise operations.
|
A new issue has been created to split the work. Let's merge this one and address other number representations in new PR. |
First,
Literal::Integerwas au64which is wrong regarding the specification (https://github.com/php/php-langspec/blob/master/spec/05-types.md#the-integer-type):Second, new test cases are added to test the limit values of integer representation (before and after overflow).
Third, only decimal will overflow to reals. The specification says:
It does not mention the parsing behavior. The Zend Engine will apply the overflow from integer to real for all “number” representations, aka binary, octal, decimal and hexadecimal. However, the
f32::from_str_radixandf64::from_str_radixfunctions have been removed from Rust std (see https://internals.rust-lang.org/t/deprecate-f-32-64-from-str-radix/2405) for good reasons I have to admit. So I consider this is an edge case and only the support for decimal overflow is added.If a volunteer woud like to reimplement a safe and stable
from_str_radixforf64(for 2, 8, 10 and 16 radices only), good! I am pretty sure it will be hard (to write and to test). If this is a very important conformance issue, let's open an issue.Thoughts @jubianchi, @nikic?