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

Curve: Check for finiteness before performing calculations in sample_baked() functions #98625

Merged

Conversation

jweisberg
Copy link
Contributor

Prevented crash in Curve2D::_find_interval() and Curve3D::_find_interval() by checking for finiteness before performing any mathematical operations. Resolves #98372. This is my first time contributing, please let me know if anything needs to be changed.

@jweisberg jweisberg requested review from a team as code owners October 28, 2024 19:55
@Chaosus Chaosus added this to the 4.4 milestone Oct 29, 2024
@AThousandShips
Copy link
Member

This looks good to me

One thing is that it might be better to put this change in the methods that call this

Partially because it makes the error message more helpful (hard to figure out what you're doing wrong if it says _find_interval if you called sample_baked)

It might also be good to check this earlier, before baking the curve if it isn't baked

But this should be good as it is, but might be good to improve

@jweisberg
Copy link
Contributor Author

Thanks for the feedback! Yeah I initially thought putting the check in the _find_interval functions made more sense because multiple functions call them, so it's less repetitive to put the checks there, but I can see how that would make it harder for the user to figure out where things started going wrong. I'll make those changes.

Copy link
Member

@AThousandShips AThousandShips left a 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, needs to fix some formatting errors to run all the checks to make sure the tests don't need to be adjusted

But would be good to get a second pair of eyes on this just to confirm

@jweisberg jweisberg changed the title Check for finiteness before performing calculations in _find_interval() Check for finiteness before performing calculations in sample_baked() functions Oct 30, 2024
Copy link
Member

@Calinou Calinou left a 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.

I suggest adding unit tests to cover this use case in tests/test_curve.h, tests/test_curve_2d.h and tests/test_curve3d.h, so we can make sure this doesn't regress in the future. See Unit testing in the documentation. You'll need to use ERR_PRINT_OFF; and ERR_PRINT_ON; to silence errors before and after the calls that would return NaN otherwise.

@@ -479,6 +479,9 @@ void Curve::set_bake_resolution(int p_resolution) {
}

real_t Curve::sample_baked(real_t p_offset) const {
// Make sure that p_offset is finite.
Copy link
Member

Choose a reason for hiding this comment

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

IMO the comment is unnecessary, the code is self-explanatory.
At least there is no reason to repeat it in every method.

Copy link
Member

@KoBeWi KoBeWi left a comment

Choose a reason for hiding this comment

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

Looks fine.
I noticed all existing error checks happen after bake(). Doesn't seem important though.

@adamscott adamscott changed the title Check for finiteness before performing calculations in sample_baked() functions Check for finiteness before performing calculations in sample_baked() functions Nov 5, 2024
@Repiteo
Copy link
Contributor

Repiteo commented Nov 10, 2024

Please squash your commits into one, see the interactive rebase for details

@akien-mga akien-mga changed the title Check for finiteness before performing calculations in sample_baked() functions Curve: Check for finiteness before performing calculations in sample_baked() functions Nov 12, 2024
@akien-mga akien-mga force-pushed the handle-nan-in-curve3d-find-interval branch from 0e5091a to 33afdff Compare November 12, 2024 14:54
@akien-mga
Copy link
Member

Please squash your commits into one, see the interactive rebase for details

Did so myself to ease the process, so this should be ready to merge.

@Repiteo Repiteo merged commit 1af1bb7 into godotengine:master Nov 12, 2024
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Nov 12, 2024

Thanks! Congratulations on your first contribution! 🎉

@jweisberg
Copy link
Contributor Author

Please squash your commits into one, see the interactive rebase for details

Did so myself to ease the process, so this should be ready to merge.

Thanks for the assist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NaN/inf not handled in Curve3D sample_baked, crashes
7 participants