1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-02 16:38:41 +00:00

fix integer overflow in mapgen

Some calculations involving the magic seed had overflow because the result of an intermediate arithmetic step could not fit in an s32. By making the magic seed unsigned, the other operand in the equation will be cast to unsigned, and possibly other operands or intermediate operands. This will result in unexpected behavior if an operand is negative, which is technically possible, but logically should not happen.
This commit is contained in:
JosiahWI 2021-09-17 11:39:45 -05:00
parent b480a3e9fd
commit 9572ad1d9f
No known key found for this signature in database
GPG key ID: C7BB8573A4ABC4B9
2 changed files with 4 additions and 2 deletions

View file

@ -238,7 +238,8 @@ u32 Mapgen::getBlockSeed(v3s16 p, s32 seed)
u32 Mapgen::getBlockSeed2(v3s16 p, s32 seed) u32 Mapgen::getBlockSeed2(v3s16 p, s32 seed)
{ {
u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed; // Unsigned magic seed prevents undefined behavior.
u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013U * seed;
n = (n >> 13) ^ n; n = (n >> 13) ^ n;
return (n * (n * n * 60493 + 19990303) + 1376312589); return (n * (n * n * 60493 + 19990303) + 1376312589);
} }

View file

@ -35,7 +35,8 @@
#define NOISE_MAGIC_X 1619 #define NOISE_MAGIC_X 1619
#define NOISE_MAGIC_Y 31337 #define NOISE_MAGIC_Y 31337
#define NOISE_MAGIC_Z 52591 #define NOISE_MAGIC_Z 52591
#define NOISE_MAGIC_SEED 1013 // Unsigned magic seed prevents undefined behavior.
#define NOISE_MAGIC_SEED 1013U
typedef float (*Interp2dFxn)( typedef float (*Interp2dFxn)(
float v00, float v10, float v01, float v11, float v00, float v10, float v01, float v11,