Skip to content

Conversation

BrzVlad
Copy link
Member

@BrzVlad BrzVlad commented Aug 25, 2021

Fixes #51323

mono_is_power_of_two is meant to be used only on unsigned numbers. In some cases we pass a signed value instead. This is typically not a problem because negative numbers are not detected as a power of two, except for the special number -2147483648, which is coded as 0x80000000, therefore a power of two.
Floating point numbers are truncated towards 0 when converted to integers, therefore we need to increase the overflow range. For example, (int8)-128.5 = -128, without overflows, even if -128.5 is smaller than the minimum integer.

Stop relying on undefined behavior by casting from floating point to integral without range checks.
Copy link
Member

@lambdageek lambdageek left a comment

Choose a reason for hiding this comment

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

Looks ok. I'd prefer to move the math to mono-math.h

Later we should reuse these methods also with jit
@BrzVlad BrzVlad merged commit 7de1d1a into dotnet:main Aug 26, 2021
@srxqds
Copy link
Contributor

srxqds commented Aug 27, 2021

it fixed some bugs, can you backport to release/6.0 branch?

@lambdageek
Copy link
Member

/backport to release/6.0

@github-actions
Copy link
Contributor

Started backporting to release/6.0: https://github.com/dotnet/runtime/actions/runs/1174509721

@ghost ghost locked as resolved and limited conversation to collaborators Sep 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Some tests for overflow checking operations are disabled on Mono
3 participants