From a99e985674f03ba977728feef003c83bda50476e Mon Sep 17 00:00:00 2001
From: sfan5 <sfan5@live.de>
Date: Thu, 23 Jan 2025 12:18:20 +0100
Subject: [PATCH] Centralize arbitrary area volume limit and raise it (#15696)

---
 src/constants.h              | 19 ++++++++++---------
 src/script/lua_api/l_env.cpp |  5 ++---
 src/voxel.cpp                |  8 +++-----
 3 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/src/constants.h b/src/constants.h
index de490cd2e..4a8ed4471 100644
--- a/src/constants.h
+++ b/src/constants.h
@@ -13,7 +13,7 @@
 */
 
 /*
-    Connection
+    Network Protocol
 */
 
 #define PEER_ID_INEXISTENT 0
@@ -60,15 +60,16 @@
 // Use floatToInt(p, BS) and intToFloat(p, BS).
 #define BS 10.0f
 
-// Dimension of a MapBlock
+// Dimension of a MapBlock in nodes
 #define MAP_BLOCKSIZE 16
-// This makes mesh updates too slow, as many meshes are updated during
-// the main loop (related to TempMods and day/night)
-//#define MAP_BLOCKSIZE 32
 
 // Player step height in nodes
 #define PLAYER_DEFAULT_STEPHEIGHT 0.6f
 
+// Arbitrary volume limit for working with contiguous areas (in nodes)
+// needs to safely fit in the VoxelArea class; used by e.g. VManips
+#define MAX_WORKING_VOLUME 150000000UL
+
 /*
     Old stuff that shouldn't be hardcoded
 */
@@ -82,6 +83,10 @@
 // Default maximal breath of a player
 #define PLAYER_MAX_BREATH_DEFAULT 10
 
+/*
+    Misc
+*/
+
 // Number of different files to try to save a player to if the first fails
 // (because of a case-insensitive filesystem)
 // TODO: Use case-insensitive player names instead of this hack.
@@ -93,8 +98,4 @@
 // the file attempting to ensure a unique filename
 #define SCREENSHOT_MAX_SERIAL_TRIES 1000
 
-/*
-    GUI related things
-*/
-
 #define TTF_DEFAULT_FONT_SIZE (16)
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp
index 5ebb2c3e4..51604fff0 100644
--- a/src/script/lua_api/l_env.cpp
+++ b/src/script/lua_api/l_env.cpp
@@ -847,9 +847,8 @@ int ModApiEnv::l_find_node_near(lua_State *L)
 void ModApiEnvBase::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");
+	if (volume > MAX_WORKING_VOLUME) {
+		throw LuaError("Area volume exceeds allowed value of " + std::to_string(MAX_WORKING_VOLUME));
 	}
 
 	// Clamp to map range to avoid problems
diff --git a/src/voxel.cpp b/src/voxel.cpp
index f74129260..4d0ce84b7 100644
--- a/src/voxel.cpp
+++ b/src/voxel.cpp
@@ -118,12 +118,10 @@ static inline void checkArea(const VoxelArea &a)
 	// won't overflow since cbrt(2^64) > 2^16
 	u64 real_volume = static_cast<u64>(a.getExtent().X) * a.getExtent().Y * a.getExtent().Z;
 
-	// Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000
-	// Note: the hard limit is somewhere around 2^31 due to s32 type
-	constexpr u64 MAX_ALLOWED = 4096000;
-	if (real_volume > MAX_ALLOWED) {
+	static_assert(MAX_WORKING_VOLUME < S32_MAX); // hard limit is somewhere here
+	if (real_volume > MAX_WORKING_VOLUME) {
 		throw BaseException("VoxelManipulator: "
-			"Area volume exceeds allowed value of " + std::to_string(MAX_ALLOWED));
+			"Area volume exceeds allowed value of " + std::to_string(MAX_WORKING_VOLUME));
 	}
 }