diff options
author | sfan5 <sfan5@live.de> | 2021-11-26 19:32:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-26 19:32:41 +0100 |
commit | 87ab97da2ace31fdb46a88a0901ec664dd666feb (patch) | |
tree | 71990e03cc72a8780e76a16e9c33cf348c7f05d0 /src/script/lua_api/l_env.cpp | |
parent | b9051386ae296a6112383725bc8bfcd96dc9a226 (diff) | |
download | minetest-87ab97da2ace31fdb46a88a0901ec664dd666feb.tar.gz minetest-87ab97da2ace31fdb46a88a0901ec664dd666feb.tar.bz2 minetest-87ab97da2ace31fdb46a88a0901ec664dd666feb.zip |
Fix find_nodes_in_area misbehaving with out-of-map coordinates (#11770)
This ensures that no overflows (side-effects) happen within the find_nodes_in_area function by limiting coordinates like done in the map generation code.
Diffstat (limited to 'src/script/lua_api/l_env.cpp')
-rw-r--r-- | src/script/lua_api/l_env.cpp | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 2c709d31b..18ee3a521 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -880,6 +880,21 @@ int ModApiEnvMod::l_find_node_near(lua_State *L) return 0; } +static void checkArea(v3s16 &minp, v3s16 &maxp) +{ + auto volume = VoxelArea(minp, maxp).getVolume(); + // Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000 + if (volume > 4096000) { + throw LuaError("Area volume exceeds allowed value of 4096000"); + } + + // Clamp to map range to avoid problems +#define CLAMP(arg) core::clamp(arg, (s16)-MAX_MAP_GENERATION_LIMIT, (s16)MAX_MAP_GENERATION_LIMIT) + minp = v3s16(CLAMP(minp.X), CLAMP(minp.Y), CLAMP(minp.Z)); + maxp = v3s16(CLAMP(maxp.X), CLAMP(maxp.Y), CLAMP(maxp.Z)); +#undef CLAMP +} + // find_nodes_in_area(minp, maxp, nodenames, [grouped]) int ModApiEnvMod::l_find_nodes_in_area(lua_State *L) { @@ -899,13 +914,7 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L) } #endif - v3s16 cube = maxp - minp + 1; - // Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000 - if ((u64)cube.X * (u64)cube.Y * (u64)cube.Z > 4096000) { - luaL_error(L, "find_nodes_in_area(): area volume" - " exceeds allowed value of 4096000"); - return 0; - } + checkArea(minp, maxp); std::vector<content_t> filter; collectNodeIds(L, 3, ndef, filter); @@ -1010,13 +1019,7 @@ int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L) } #endif - v3s16 cube = maxp - minp + 1; - // Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000 - if ((u64)cube.X * (u64)cube.Y * (u64)cube.Z > 4096000) { - luaL_error(L, "find_nodes_in_area_under_air(): area volume" - " exceeds allowed value of 4096000"); - return 0; - } + checkArea(minp, maxp); std::vector<content_t> filter; collectNodeIds(L, 3, ndef, filter); |