1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-21 18:11:11 +00:00

JSON: Support consistent larger max. depth of 1024

This commit is contained in:
Lars Mueller 2024-10-14 15:46:25 +02:00 committed by Lars Müller
parent 4c419c4020
commit e2ea359925
4 changed files with 30 additions and 8 deletions

View file

@ -2149,11 +2149,11 @@ bool push_json_value(lua_State *L, const Json::Value &value, int nullindex)
}
// Converts Lua table --> JSON
void read_json_value(lua_State *L, Json::Value &root, int index, u8 recursion)
void read_json_value(lua_State *L, Json::Value &root, int index, u16 max_depth)
{
if (recursion > 16) {
throw SerializationError("Maximum recursion depth exceeded");
}
if (max_depth == 0)
throw SerializationError("depth exceeds MAX_JSON_DEPTH");
int type = lua_type(L, index);
if (type == LUA_TBOOLEAN) {
root = (bool) lua_toboolean(L, index);
@ -2164,11 +2164,13 @@ void read_json_value(lua_State *L, Json::Value &root, int index, u8 recursion)
const char *str = lua_tolstring(L, index, &len);
root = std::string(str, len);
} else if (type == LUA_TTABLE) {
// Reserve two slots for key and value.
lua_checkstack(L, 2);
lua_pushnil(L);
while (lua_next(L, index)) {
// Key is at -2 and value is at -1
Json::Value value;
read_json_value(L, value, lua_gettop(L), recursion + 1);
read_json_value(L, value, lua_gettop(L), max_depth - 1);
Json::ValueType roottype = root.type();
int keytype = lua_type(L, -1);

View file

@ -200,7 +200,7 @@ bool push_json_value (lua_State *L,
const Json::Value &value,
int nullindex);
void read_json_value (lua_State *L, Json::Value &root,
int index, u8 recursion = 0);
int index, u16 max_depth);
/*!
* Pushes a Lua `pointed_thing` to the given Lua stack.