fix potential null dereferences on incorrect block.* functions use & refactor

This commit is contained in:
MihailRis
2024-11-06 02:55:37 +03:00
parent 8ef7d43603
commit 961773c9f9
3 changed files with 43 additions and 19 deletions
+13 -7
View File
@@ -68,18 +68,18 @@ static int l_is_segment(lua::State* L) {
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto vox = level->chunks->get(x, y, z);
return lua::pushboolean(L, vox->state.segment);
const auto& vox = level->chunks->require(x, y, z);
return lua::pushboolean(L, vox.state.segment);
}
static int l_seek_origin(lua::State* L) {
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto vox = level->chunks->get(x, y, z);
auto& def = indices->blocks.require(vox->id);
const auto& vox = level->chunks->require(x, y, z);
auto& def = indices->blocks.require(vox.id);
return lua::pushivec_stack(
L, level->chunks->seekOrigin({x, y, z}, def, vox->state)
L, level->chunks->seekOrigin({x, y, z}, def, vox.state)
);
}
@@ -223,6 +223,9 @@ static int l_get_user_bits(lua::State* L) {
if (def.rt.extended) {
auto origin = level->chunks->seekOrigin({x, y, z}, def, vox->state);
vox = level->chunks->get(origin.x, origin.y, origin.z);
if (vox == nullptr) {
return lua::pushinteger(L, 0);
}
}
uint mask = ((1 << bits) - 1) << offset;
uint data = (blockstate2int(vox->state) & mask) >> offset;
@@ -251,6 +254,9 @@ static int l_set_user_bits(lua::State* L) {
if (def.rt.extended) {
auto origin = level->chunks->seekOrigin({x, y, z}, def, vox->state);
vox = level->chunks->get(origin);
if (vox == nullptr) {
return 0;
}
}
vox->state.userbits = (vox->state.userbits & (~mask)) | value;
chunk->setModifiedAndUnsaved();
@@ -487,7 +493,6 @@ static int l_get_field(lua::State* L) {
if (lua::gettop(L) >= 5) {
index = lua::tointeger(L, 5);
}
auto vox = level->chunks->get(x, y, z);
auto cx = floordiv(x, CHUNK_W);
auto cz = floordiv(z, CHUNK_D);
auto chunk = level->chunks->getChunk(cx, cz);
@@ -495,7 +500,8 @@ static int l_get_field(lua::State* L) {
auto lz = z - cz * CHUNK_W;
size_t voxelIndex = vox_index(lx, y, lz);
const auto& def = content->getIndices()->blocks.require(vox->id);
const auto& vox = level->chunks->require(x, y, z);
const auto& def = content->getIndices()->blocks.require(vox.id);
if (def.dataStruct == nullptr) {
return 0;
}