aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkwolekr <kwolekr@minetest.net>2014-12-12 02:46:52 -0500
committerkwolekr <kwolekr@minetest.net>2014-12-12 02:46:52 -0500
commit4e5d17f666c1321aa47fad8d315a73f64ff8ca4b (patch)
tree329a3af72a68442149c4597de77c61e6273261b9
parent00fc0babe0c6c5464fa9ffbc5a257b1e2aa93111 (diff)
downloadminetest-4e5d17f666c1321aa47fad8d315a73f64ff8ca4b.tar.gz
minetest-4e5d17f666c1321aa47fad8d315a73f64ff8ca4b.tar.bz2
minetest-4e5d17f666c1321aa47fad8d315a73f64ff8ca4b.zip
LuaPerlinNoiseMap: Prevent invalid memory access when attempting to generate 3d noise with a buffer created for 2d
-rw-r--r--src/script/lua_api/l_noise.cpp24
-rw-r--r--src/script/lua_api/l_noise.h4
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[];