Skip to content

Add safe integer range validation to parseInt, parseOctal, and parseHex#5

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-cb476eaa-f4e9-4d75-9f9d-187e8c1f015d
Draft

Add safe integer range validation to parseInt, parseOctal, and parseHex#5
Copilot wants to merge 3 commits intomainfrom
copilot/fix-cb476eaa-f4e9-4d75-9f9d-187e8c1f015d

Conversation

Copy link
Copy Markdown

Copilot AI commented Oct 5, 2025

Summary

This PR adds validation to ensure that integers parsed by std.parseInt(), std.parseOctal(), and std.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:

// This would parse without error, but loses precision
std.parseInt('9007199254740992')  // 2^53

Solution

Following the implementation in sjsonnet PR #381, this PR adds validation in both the Rust and Jsonnet implementations:

Safe Integer Range

  • MAX_SAFE_INTEGER: 9,007,199,254,740,991 (2^53 - 1)
  • MIN_SAFE_INTEGER: -9,007,199,254,740,991 (-(2^53 - 1))

Changes

  1. src/stdlib.rs: Updated parse_int() to validate the parsed number is within safe range and is finite (not NaN/Infinity)

  2. src/google_stdlib.jsonnet: Updated parseInt(), parseOctal(), and parseHex() to assert that parsed values are within the safe integer range

  3. Tests: Added validation tests for boundary values (MAX_SAFE_INTEGER and MIN_SAFE_INTEGER) and error test cases for overflow/underflow scenarios

Example Behavior

// These now work correctly - values within safe range
std.parseInt('9007199254740991')    // ✓ Returns 9007199254740991
std.parseInt('-9007199254740991')   // ✓ Returns -9007199254740991

// These now properly reject values outside safe range
std.parseInt('9007199254740992')    // ✗ Error: numeric value outside safe integer range
std.parseInt('-9007199254740992')   // ✗ Error: numeric value outside safe integer range

Testing

  • All existing tests continue to pass
  • Added tests for MAX_SAFE_INTEGER and MIN_SAFE_INTEGER boundary values
  • Added error test cases for values beyond the safe range

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

This section details on the original issue you should resolve

<issue_title>Should test if the integer is in safe range.</issue_title>
<issue_description>Currently, just create the number directly from str.
refs: databricks/sjsonnet#381</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #3

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits October 5, 2025 20:29
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
Copilot AI requested a review from com-junkawasaki October 5, 2025 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should test if the integer is in safe range.

2 participants