Skip to content
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

fix: add safe double-to-int64 conversion for bitwise operations #1217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thevilledev
Copy link

@thevilledev thevilledev commented Mar 3, 2025

Implements proper safety checks for all bitwise operations (<<, >>, &, ^, |) when converting double values to int64_t. This prevents undefined behavior and potential security issues from integer overflow or invalid values.

The implementation:

  • Adds safeDoubleToInt64 function with proper range validation
  • Applies this function to all bitwise operations
  • Limits values to the safe integer range (±(2^53-1))
  • Provides clear error messages with source location context

The safe integer range limitation is necessary because IEEE 754 doubles can only precisely represent integers up to 2^53-1. Beyond this range, precision is lost, which would lead to unpredictable results in bitwise operations that depend on exact bit patterns.

This change aligns with the Jsonnet specification which requires bitwise operations to convert operands to signed 64-bit integers before performing operations, while ensuring mathematical correctness.

NOTE: This was originally reported through the Google OSS VRP. Conclusion was that this can be reported publicly.

Copy link

google-cla bot commented Mar 3, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@thevilledev
Copy link
Author

Unable to sign the CLA:

A server error occurred, please try your request again.

I will try later.

Implements proper safety checks for all bitwise operations (<<, >>, &, ^, |)
when converting double values to int64_t. This prevents undefined behavior
and potential security issues from integer overflow or invalid values.

The implementation:
- Adds safeDoubleToInt64 function with proper range validation
- Applies this function to all bitwise operations
- Limits values to the safe integer range (±(2^53-1))
- Provides clear error messages with source location context

The safe integer range limitation is necessary because IEEE 754 doubles
can only precisely represent integers up to 2^53-1. Beyond this range,
precision is lost, which would lead to unpredictable results in bitwise
operations that depend on exact bit patterns.

This change aligns with the Jsonnet specification which requires bitwise
operations to convert operands to signed 64-bit integers before performing
operations, while ensuring mathematical correctness.

Signed-off-by: Ville Vesilehto <[email protected]>
@thevilledev thevilledev force-pushed the fix/bitwise-double-to-int64 branch from 715e678 to 42e89a4 Compare March 3, 2025 21:10
@thevilledev
Copy link
Author

CLA signed.

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.

2 participants