saturate overflowing numeric string casts - #84
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In php 8.x, casting a numeric string in float or scientific notation that exceeds the 64-bit integer range to
(int)(or viaintval()) saturates toPHP_INT_MAX(9223372036854775807) for positive overflow andPHP_INT_MIN(-9223372036854775808) for negative overflowPreviously,
Value.parseLeadingInt()insrc/runtime/value.zigreturned0when float strings overflowedmax_f, causing incorrect int conversions such as:(int)"1e100"returning0instead of9223372036854775807(int)"-1e100"returning0instead of-9223372036854775808(int)"1e19"returning0instead of9223372036854775807(int)"9.9e18"returning0instead of9223372036854775807While pure integer digit strings (such as
(int)"99999999999999999999") already saturated correctly tomaxInt(i64)/minInt(i64), the float/exponent path was missing this behavior and returning0Solution
f >= max_f) tostd.math.maxInt(i64)inValue.parseLeadingIntf <= -max_f) tostd.math.minInt(i64)inValue.parseLeadingIntTests
src/runtime/value.zigcovering positive overflow, negative overflow, boundary values, and in-range float conversionstests/numeric_string_int_saturation.phpmatching native PHP 8.x behavior