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

Signed integer overflow _fnlHash2D #140

Open
Vaxeral opened this issue May 13, 2024 · 2 comments
Open

Signed integer overflow _fnlHash2D #140

Vaxeral opened this issue May 13, 2024 · 2 comments

Comments

@Vaxeral
Copy link

Vaxeral commented May 13, 2024

Hi. The library seems to have undefined behavior, specifically signed integer overflow.
include/FastNoiseLite.h:484:10: runtime error: signed integer overflow: 1337 * 668265261 cannot be represented in type 'int'

static inline int _fnlHash2D(int seed, int xPrimed, int yPrimed)
{
    int hash = seed ^ xPrimed ^ yPrimed;

    hash *= 0x27d4eb2d;
    return hash;
}

Is this intended, if so what is the reason?

@Auburn
Copy link
Owner

Auburn commented May 14, 2024

The overflow is intended it's part of the hashing algorithm

@Vaxeral
Copy link
Author

Vaxeral commented May 14, 2024

Right and it will work almost always.
There are cases where signed integer overflow being undefined behavior can cause confusion.

int foo(int x) {
    return x+1>x;
}

The optimizer will take advantage of this fact and return 1 regardless of the value x + 1 which may overflow.

But more importantly direclty importing the c library into a zig code base is affected by this issue. Because zig is very strict about violating undefined behavior the example fails in debug builds. This can easily be fixed on zig side by using the overflow operator. Just thought I'd share.

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

No branches or pull requests

2 participants