From 4e5d17f666c1321aa47fad8d315a73f64ff8ca4b Mon Sep 17 00:00:00 2001 From: kwolekr Date: Fri, 12 Dec 2014 02:46:52 -0500 Subject: [PATCH] LuaPerlinNoiseMap: Prevent invalid memory access when attempting to generate 3d noise with a buffer created for 2d --- src/script/lua_api/l_noise.cpp | 24 ++++++++++-------------- src/script/lua_api/l_noise.h | 4 ++-- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/script/lua_api/l_noise.cpp b/src/script/lua_api/l_noise.cpp index f4ae3fb08..a9b59791f 100644 --- a/src/script/lua_api/l_noise.cpp +++ b/src/script/lua_api/l_noise.cpp @@ -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()); } diff --git a/src/script/lua_api/l_noise.h b/src/script/lua_api/l_noise.h index 6e3029aef..3e22ac7a0 100644 --- a/src/script/lua_api/l_noise.h +++ b/src/script/lua_api/l_noise.h @@ -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[];