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 overflow on integer casting on modulo operator #2806

Closed
wants to merge 1 commit into from

Conversation

itchyny
Copy link
Contributor

@itchyny itchyny commented Aug 1, 2023

I noticed that pow(2, 63) is unsafe to cast to int. I was carefully flipped the sign, but failed to notice the boundary bug. Ref: #2797

@itchyny itchyny changed the title Fix truncation on integer casting on modulo operator Fix overflow on integer casting on modulo operator Aug 1, 2023
@nicowilliams
Copy link
Contributor

Did that raise a warning somewhere?

@@ -396,7 +396,7 @@ static jv f_divide(jq_state *jq, jv input, jv a, jv b) {
}
}

#define dtoi(n) ((n) < INTMAX_MIN ? INTMAX_MIN : -(n) < INTMAX_MIN ? INTMAX_MAX : (intmax_t)(n))
#define dtoi(n) ((n) < INTMAX_MIN ? INTMAX_MIN : -(n) <= INTMAX_MIN ? INTMAX_MAX : (intmax_t)(n))
Copy link
Contributor

@nicowilliams nicowilliams Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this when reviewing the original, but n is a double, so I don't think there's a problem here because INTMAX_MIN should be promoted to double.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But n = 9223372036854775808.0 = pow(2, 63) exceeds INTMAX_MAX = 9223372036854775807, thus unsafe to cast, and -n < INTMAX_MIN = -9223372036854775808 (exactly in double) is falsy. Assuming INTX_MAX == -1-INTX_MIN, casting a double n overflows when n >= INTX_MAX+1 i.e. n >= -INTX_MIN i.e. -n <= INTX_MIN (the last one is only safe comparison in double).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But INTMAX_MAX here gets promoted to a double, so it should be safe.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you get a warning with UBSAN?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I just noticed while renaming the macro (you were faster though). I originally referred to https://stackoverflow.com/a/60801229/1363488.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so we can't represent INTMAX_MIN and INTMAX_MAX exactly as doubles, but then changing the comparison from < to <= won't make much difference. We're relying on the cast from double to intmax_t to be safe.

https://stackoverflow.com/questions/51104995/can-a-conversion-from-double-to-int-be-written-in-portable-c

If we want to be extra careful we should probably have global read-only double constants that represent the smallest and largest integer that can be represented exactly in double, but being careful to allow double to not be an IEEE 754 64-bit double.

@@ -235,7 +235,7 @@ static block constant_fold(block a, block b, int op) {
res = jv_number(na / nb);
break;
case '%':
#define is_unsafe_to_int_cast(n) (isnan(n) || (n) < INTMAX_MIN || -(n) < INTMAX_MIN)
#define is_unsafe_to_int_cast(n) (isnan(n) || (n) < INTMAX_MIN || -(n) <= INTMAX_MIN)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@itchyny itchyny marked this pull request as ready for review August 1, 2023 11:36
@itchyny itchyny added this to the 1.7 release milestone Aug 1, 2023
@itchyny itchyny added the bug label Aug 1, 2023
@itchyny itchyny closed this Aug 2, 2023
@itchyny itchyny deleted the fix-cast-overflow-modulo branch August 2, 2023 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants