Add safe integer range validation to parseInt, parseOctal, and parseHex#5
Draft
Add safe integer range validation to parseInt, parseOctal, and parseHex#5
Conversation
Co-authored-by: com-junkawasaki <623495+com-junkawasaki@users.noreply.github.com>
Co-authored-by: com-junkawasaki <623495+com-junkawasaki@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Should test if the integer is in safe range.
Add safe integer range validation to parseInt, parseOctal, and parseHex
Oct 5, 2025
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.
Summary
This PR adds validation to ensure that integers parsed by
std.parseInt(),std.parseOctal(), andstd.parseHex()are within the safe range for IEEE 754 double-precision floating-point numbers.Problem
Currently, these parsing functions create numbers directly from strings without checking if they're within the safe integer range. IEEE 754 double-precision floating-point numbers can only precisely represent integers up to 2^53 - 1 (9,007,199,254,740,991). Beyond this range, precision is lost, which can lead to incorrect results in bitwise operations and other integer operations.
For example:
Solution
Following the implementation in sjsonnet PR #381, this PR adds validation in both the Rust and Jsonnet implementations:
Safe Integer Range
Changes
src/stdlib.rs: Updatedparse_int()to validate the parsed number is within safe range and is finite (not NaN/Infinity)src/google_stdlib.jsonnet: UpdatedparseInt(),parseOctal(), andparseHex()to assert that parsed values are within the safe integer rangeTests: Added validation tests for boundary values (MAX_SAFE_INTEGER and MIN_SAFE_INTEGER) and error test cases for overflow/underflow scenarios
Example Behavior
Testing
Impact
This change improves correctness by preventing precision loss that can occur when parsing very large integers. Applications that rely on precise integer arithmetic will benefit from this validation.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.