Skip to content

Conversation

paulgessinger
Copy link
Member

@paulgessinger paulgessinger commented Aug 22, 2025

There's a lot of very easily triggered UB in MathHelpers.hpp. This PR tries to avoid some of it, and cover it with tests.

--- END COMMIT MESSAGE ---

Disclosure: some help in the tests from Claude.

@junggjo9

@paulgessinger paulgessinger added this to the next milestone Aug 22, 2025
@@ -27,17 +33,10 @@ constexpr T abs(const T n) {
/// @brief Calculates the ordinary power of the number x.
/// @param x: Number to take the power from
/// @param p: Power to take
template <typename T, std::integral P>
template <typename T, std::unsigned_integral P>
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really want to remove the possibility to have pow(x, -1)?

Copy link
Contributor

Choose a reason for hiding this comment

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

I remember, no we need to allow for negative exponents. For example, in the Chebychev polynomials

pow(2., static_cast<int>(n - 2 * k - 1));
, this expression can have a negative exponent

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, but the way this is implemented, negative exponents are not safe, and can easily pop over into UB. I've found it to even segfault (probably due to stack overflow) with a negative exponent and a sufficiently small base.

Copy link
Member Author

@paulgessinger paulgessinger Aug 22, 2025

Choose a reason for hiding this comment

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

Concretely, I got it to segfault on pow(1e-300, -1). At the very least, I would like to add a runtime check that the base is not too small.

@github-actions github-actions bot added Component - Core Affects the Core module Seeding labels Aug 22, 2025
@paulgessinger paulgessinger marked this pull request as draft August 27, 2025 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component - Core Affects the Core module Seeding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants