Modernize client code (#6250)
[oweals/minetest.git] / src / noise.cpp
index e68c5ce168648a6c9e32c3ff7e3bf69bb888de8f..e6ca8a4951cf669c8312275f2b859fc34d2f1297 100644 (file)
@@ -130,7 +130,9 @@ s32 PcgRandom::range(s32 min, s32 max)
        if (max < min)
                throw PrngException("Invalid range (max < min)");
 
-       u32 bound = max - min + 1;
+       // We have to cast to s64 because otherwise this could overflow,
+       // and signed overflow is undefined behavior.
+       u32 bound = (s64)max - (s64)min + 1;
        return range(bound) + min;
 }
 
@@ -713,7 +715,8 @@ float *Noise::perlinMap2D(float x, float y, float *persistence_map)
        if (persistence_map) {
                if (!persist_buf)
                        persist_buf = new float[bufsize];
-               memset(persist_buf, 1.0f, sizeof(float) * bufsize);
+               for (size_t i = 0; i != bufsize; i++)
+                       persist_buf[i] = 1.0;
        }
 
        for (size_t oct = 0; oct < np.octaves; oct++) {
@@ -750,8 +753,8 @@ float *Noise::perlinMap3D(float x, float y, float z, float *persistence_map)
        if (persistence_map) {
                if (!persist_buf)
                        persist_buf = new float[bufsize];
-
-               memset(persist_buf, 1.0f, sizeof(float) * bufsize);
+               for (size_t i = 0; i != bufsize; i++)
+                       persist_buf[i] = 1.0;
        }
 
        for (size_t oct = 0; oct < np.octaves; oct++) {