mirror of
https://github.com/minetest/minetest.git
synced 2024-11-23 08:03:45 +01:00
Fix undefined behaviors (#14365)
* Initialize member `floats` in ContentFeatures * Do not assign big double to u32 * Do not assign negative floating point number to unsigned integer
This commit is contained in:
parent
3cac17d23e
commit
6cbb9193ea
@ -297,7 +297,11 @@ Biome *BiomeGenOriginal::calcBiomeFromNoise(float heat, float humidity, v3s16 po
|
|||||||
// Carefully tune pseudorandom seed variation to avoid single node dither
|
// Carefully tune pseudorandom seed variation to avoid single node dither
|
||||||
// and create larger scale blending patterns similar to horizontal biome
|
// and create larger scale blending patterns similar to horizontal biome
|
||||||
// blend.
|
// blend.
|
||||||
const u64 seed = pos.Y + (heat + humidity) * 0.9f;
|
// The calculation can be a negative floating point number, which is an
|
||||||
|
// undefined behavior if assigned to unsigned integer. Cast the result
|
||||||
|
// into signed integer before it is casted into unsigned integer to
|
||||||
|
// eliminate the undefined behavior.
|
||||||
|
const u64 seed = static_cast<s64>(pos.Y + (heat + humidity) * 0.9f);
|
||||||
PcgRandom rng(seed);
|
PcgRandom rng(seed);
|
||||||
|
|
||||||
if (biome_closest_blend && dist_min_blend <= dist_min &&
|
if (biome_closest_blend && dist_min_blend <= dist_min &&
|
||||||
|
@ -356,6 +356,8 @@ void ContentFeatures::reset()
|
|||||||
has_on_construct = false;
|
has_on_construct = false;
|
||||||
has_on_destruct = false;
|
has_on_destruct = false;
|
||||||
has_after_destruct = false;
|
has_after_destruct = false;
|
||||||
|
floats = false;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Actual data
|
Actual data
|
||||||
|
|
||||||
|
@ -416,8 +416,7 @@ DigParams getDigParams(const ItemGroupList &groups,
|
|||||||
// The actual number of uses increases
|
// The actual number of uses increases
|
||||||
// exponentially with leveldiff.
|
// exponentially with leveldiff.
|
||||||
// If the levels are equal, real_uses equals cap.uses.
|
// If the levels are equal, real_uses equals cap.uses.
|
||||||
u32 real_uses = cap.uses * pow(3.0, leveldiff);
|
const u32 real_uses = std::min<f64>(cap.uses * pow(3.0, leveldiff), U16_MAX);
|
||||||
real_uses = MYMIN(real_uses, U16_MAX);
|
|
||||||
result_wear = calculateResultWear(real_uses, initial_wear);
|
result_wear = calculateResultWear(real_uses, initial_wear);
|
||||||
result_main_group = groupname;
|
result_main_group = groupname;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user