LuaPerlinNoiseMap: Prevent invalid memory access when attempting to generate 3d noise...
authorkwolekr <kwolekr@minetest.net>
Fri, 12 Dec 2014 07:46:52 +0000 (02:46 -0500)
committerkwolekr <kwolekr@minetest.net>
Fri, 12 Dec 2014 07:46:52 +0000 (02:46 -0500)
src/script/lua_api/l_noise.cpp
src/script/lua_api/l_noise.h

index f4ae3fb08fb5e5ea205a09cd3f37a56c08cef0ac..a9b59791f88b2cba044c3ad0c572fe0b56ef5267 100644 (file)
@@ -60,17 +60,6 @@ LuaPerlinNoise::LuaPerlinNoise(NoiseParams *params) :
 }
 
 
-/*
-LuaPerlinNoise::LuaPerlinNoise(int a_seed, int a_octaves,
-       float a_persistence, float a_scale)
-{
-       np.seed    = a_seed;
-       np.octaves = a_octaves;
-       np.persist = a_persistence;
-       np.spread  = v3f(a_scale, a_scale, a_scale);
-}
-*/
-
 LuaPerlinNoise::~LuaPerlinNoise()
 {
 }
@@ -215,6 +204,9 @@ int LuaPerlinNoiseMap::l_get3dMap(lua_State *L)
        LuaPerlinNoiseMap *o = checkobject(L, 1);
        v3f p = read_v3f(L, 2);
 
+       if (!o->m_is3d)
+               return 0;
+
        Noise *n = o->noise;
        n->perlinMap3D(p.X, p.Y, p.Z);
 
@@ -242,6 +234,9 @@ int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L)
        LuaPerlinNoiseMap *o = checkobject(L, 1);
        v3f p = read_v3f(L, 2);
 
+       if (!o->m_is3d)
+               return 0;
+
        Noise *n = o->noise;
        n->perlinMap3D(p.X, p.Y, p.Z);
 
@@ -256,11 +251,12 @@ int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L)
 }
 
 
-LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *np, int seed, v3s16 size)
+LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *params, int seed, v3s16 size)
 {
-       memcpy(&m_noise_params, np, sizeof(m_noise_params));
+       m_is3d = size.Z <= 1;
+       np = *params;
        try {
-               noise = new Noise(&m_noise_params, seed, size.X, size.Y, size.Z);
+               noise = new Noise(&np, seed, size.X, size.Y, size.Z);
        } catch (InvalidNoiseParamsException &e) {
                throw LuaError(e.what());
        }
index 6e3029aef800558d81cfc2e27aebfd6f428fbd3d..3e22ac7a0a7199db213cd591e800ca99fcb46784 100644 (file)
@@ -30,7 +30,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 class LuaPerlinNoise : public ModApiBase {
 private:
        NoiseParams np;
-
        static const char className[];
        static const luaL_reg methods[];
 
@@ -59,8 +58,9 @@ public:
        LuaPerlinNoiseMap
 */
 class LuaPerlinNoiseMap : public ModApiBase {
-       NoiseParams m_noise_params;
+       NoiseParams np;
        Noise *noise;
+       bool m_is3d;
        static const char className[];
        static const luaL_reg methods[];