-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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 potential integer underflow in rounded up divisions #80390
Conversation
To link it use just "fixes", also don't write "bugsquad", that means it was added by one of the members of that team |
Please squash your commits into one, see here |
Done |
How about adding comments about the parameter limits/requirements? For example the first function requires that the sum of num and den must be between min_int and max_int (otherwise there in an overflow). |
Would there be any benefit to marking the function as constexpr? |
No I'd say, none of the contexts where it is used are constexpr compatible, all uses are runtime, and since it's always inlined it wouldn't change anything anyway |
Well right now they're not used in a constexpr context but do we know that's not going to change? And adding it wouldn't change a thing if it has to be evaluated at runtime anyway |
Ok what the hell, I didn't close this |
But if you're doing things at compile time then you should probably not be depending on this right?
|
You closed this, you probably clicked the wrong button when commenting |
Well that's great, thanks github...
In theory no, but then why does anyone use constexpr right? Arguably, for code readability so that it's still obvious what you're doing. I agree it's unlikely it will actually get used, I just think it doesn't hurt |
As this PR is being made because previous implementation bugged when num was 0, I would suggest making unit tests to prove that now this works. |
Except it's inlined so it will have no effect what so ever, and it doesn't help readability either, and no other function in this header has |
Will do |
Regarding overflow, we often just don't handle it (for instance even the simplest expression But if you wanted some cheap checks, if you look at
So you could have e.g. This will print a one time error (so as not to spam) if overflow is detected, but is compiled out in release builds so is free. |
So you'd suggest not adding comments and adding the cheap tests instead? |
To ensure the correct behaviour of these I'd say test cases should be added to |
As I said, I will. In fact I have, the changes just aren't pushed yet. |
Wanted to make sure as there was talk of different tests going on |
Potential case for this: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me from a cursory look. Sorry for the delay reviewing.
@EddieBreeg Will you be available to address the feedback? This PR also needs a rebase. |
I rebased the PR myself, solving the conflicts, adding the change I suggested, and handling a few other occurrences which I found with this regex:
There might be more when they're multiline of with either operand being a local variable, but this should be a good start. |
08d169f
to
38dc4a9
Compare
A new `Math::division_round_up()` function was added, allowing for easy and correct computation of integer divisions when the result needs to be rounded up. Fixes godotengine#80358. Co-authored-by: Rémi Verschelde <[email protected]>
Thanks! And congrats for your first merged Godot contribution 🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR was merged without even being tested. I am not happy about it.
@@ -1033,7 +1033,7 @@ void main() { | |||
|
|||
if (local == ivec3(0) && store_position_count > 0) { | |||
store_from_index = atomicAdd(dispatch_data.total_count, store_position_count); | |||
uint group_count = (store_from_index + store_position_count - 1) / 64 + 1; | |||
uint group_count = Math::division_round_up(store_from_index + store_position_count, 64); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not have been merged, the shader does not even compile.
Was this PR even tested?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My mistake, I added this yesterday and didn't notice it was touching a .glsl file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed by 7abaac6.
My mistake, I added this when rebasing and didn't notice that it was a .glsl file and thus the availability of Math was not confirmed by the C++ compiler.
I tested, but didn't notice the error. My test project was likely not using SDFGI. Anyway, this was my bad, I added this change yesterday after rebasing and noticing a few new cases of |
My mistake, I added this when rebasing and didn't notice that it was a .glsl file and thus the availability of Math was not confirmed by the C++ compiler.
A new division_round_up function was added in the Math class, allowing for easy and correct computation of integer divisions when the result needs to be rounded up.
https://github.com/EddieBreeg/godot/blob/8349f16f316b90f705da1219f52828a0af889907/core/math/math_funcs.h#L190-L201