refactor: PVS-Studio warnings fixes

This commit is contained in:
MihailRis 2024-08-04 01:30:52 +03:00
parent 245b39be62
commit 47db626145
14 changed files with 81 additions and 69 deletions

View File

@ -82,6 +82,10 @@ public:
return defs.size(); return defs.size();
} }
inline const auto& getIterable() const {
return defs;
}
inline const T* const* getDefs() const { inline const T* const* getDefs() const {
return defs.data(); return defs.data();
} }

View File

@ -37,8 +37,8 @@ public:
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
indices.push_back(i); indices.push_back(i);
} }
for (size_t i = 0; i < unitIndices.count(); i++) { for (auto unit : unitIndices.getIterable()) {
names.push_back(unitIndices.get(i)->name); //FIXME: .get(i) potentional null pointer //-V522 names.push_back(unit->name);
} }
for (size_t i = unitIndices.count(); i < count; i++) { for (size_t i = unitIndices.count(); i < count; i++) {
names.emplace_back(""); names.emplace_back("");

View File

@ -103,9 +103,8 @@ template <class T>
static void write_indices( static void write_indices(
const ContentUnitIndices<T>& indices, dynamic::List& list const ContentUnitIndices<T>& indices, dynamic::List& list
) { ) {
size_t count = indices.count(); for (auto unit : indices.getIterable()) {
for (size_t i = 0; i < count; i++) { list.put(unit->name);
list.put(indices.get(i)->name); //FIXME: .get(i) potential null pointer //-V522
} }
} }

View File

@ -17,10 +17,11 @@ ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) : conte
sideregions = std::make_unique<UVRegion[]>(indices->blocks.count() * 6); sideregions = std::make_unique<UVRegion[]>(indices->blocks.count() * 6);
auto atlas = assets->get<Atlas>("blocks"); auto atlas = assets->get<Atlas>("blocks");
for (uint i = 0; i < indices->blocks.count(); i++) { const auto& blocks = indices->blocks.getIterable();
auto def = indices->blocks.getWriteable(i); //FIXME: Potential null pointer for (uint i = 0; i < blocks.size(); i++) {
auto def = blocks[i];
for (uint side = 0; side < 6; side++) { for (uint side = 0; side < 6; side++) {
const std::string& tex = def->textureFaces[side]; //-V522 const std::string& tex = def->textureFaces[side];
if (atlas->has(tex)) { if (atlas->has(tex)) {
sideregions[i * 6 + side] = atlas->get(tex); sideregions[i * 6 + side] = atlas->get(tex);
} else if (atlas->has(TEXTURE_NOTFOUND)) { } else if (atlas->has(TEXTURE_NOTFOUND)) {

View File

@ -22,30 +22,30 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
Shader* shader, Shader* shader,
Framebuffer* fbo, Framebuffer* fbo,
Batch3D* batch, Batch3D* batch,
const Block* def, const Block& def,
int size int size
){ ){
Window::clear(); Window::clear();
blockid_t id = def->rt.id; blockid_t id = def.rt.id;
const UVRegion texfaces[6]{cache->getRegion(id, 0), cache->getRegion(id, 1), const UVRegion texfaces[6]{cache->getRegion(id, 0), cache->getRegion(id, 1),
cache->getRegion(id, 2), cache->getRegion(id, 3), cache->getRegion(id, 2), cache->getRegion(id, 3),
cache->getRegion(id, 4), cache->getRegion(id, 5)}; cache->getRegion(id, 4), cache->getRegion(id, 5)};
glm::vec3 offset(0.1f, 0.5f, 0.1f); glm::vec3 offset(0.1f, 0.5f, 0.1f);
switch (def->model) { switch (def.model) {
case BlockModel::none: case BlockModel::none:
// something went wrong... // something went wrong...
break; break;
case BlockModel::block: case BlockModel::block:
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset)); shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
batch->blockCube(glm::vec3(size * 0.63f), texfaces, batch->blockCube(glm::vec3(size * 0.63f), texfaces,
glm::vec4(1.0f), !def->rt.emissive); glm::vec4(1.0f), !def.rt.emissive);
batch->flush(); batch->flush();
break; break;
case BlockModel::aabb: case BlockModel::aabb:
{ {
glm::vec3 hitbox {}; glm::vec3 hitbox {};
for (const auto& box : def->hitboxes) { for (const auto& box : def.hitboxes) {
hitbox = glm::max(hitbox, box.size()); hitbox = glm::max(hitbox, box.size());
} }
offset = glm::vec3(1, 1, 0.0f); offset = glm::vec3(1, 1, 0.0f);
@ -55,7 +55,7 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
-hitbox * scaledSize * 0.5f * glm::vec3(1,1,-1), -hitbox * scaledSize * 0.5f * glm::vec3(1,1,-1),
hitbox * scaledSize, hitbox * scaledSize,
texfaces, glm::vec4(1.0f), texfaces, glm::vec4(1.0f),
!def->rt.emissive !def.rt.emissive
); );
} }
batch->flush(); batch->flush();
@ -64,32 +64,32 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
{ {
glm::vec3 pmul = glm::vec3(size * 0.63f); glm::vec3 pmul = glm::vec3(size * 0.63f);
glm::vec3 hitbox = glm::vec3(); glm::vec3 hitbox = glm::vec3();
for (const auto& box : def->modelBoxes) { for (const auto& box : def.modelBoxes) {
hitbox = glm::max(hitbox, box.size()); hitbox = glm::max(hitbox, box.size());
} }
offset.y += (1.0f - hitbox).y * 0.5f; offset.y += (1.0f - hitbox).y * 0.5f;
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset)); shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
for (size_t i = 0; i < def->modelBoxes.size(); i++) { for (size_t i = 0; i < def.modelBoxes.size(); i++) {
const UVRegion (&boxtexfaces)[6] = { const UVRegion (&boxtexfaces)[6] = {
def->modelUVs[i * 6], def.modelUVs[i * 6],
def->modelUVs[i * 6 + 1], def.modelUVs[i * 6 + 1],
def->modelUVs[i * 6 + 2], def.modelUVs[i * 6 + 2],
def->modelUVs[i * 6 + 3], def.modelUVs[i * 6 + 3],
def->modelUVs[i * 6 + 4], def.modelUVs[i * 6 + 4],
def->modelUVs[i * 6 + 5] def.modelUVs[i * 6 + 5]
}; };
batch->cube( batch->cube(
def->modelBoxes[i].a * glm::vec3(1.0f, 1.0f, -1.0f) * pmul, def.modelBoxes[i].a * glm::vec3(1.0f, 1.0f, -1.0f) * pmul,
def->modelBoxes[i].size() * pmul, def.modelBoxes[i].size() * pmul,
boxtexfaces, glm::vec4(1.0f), !def->rt.emissive boxtexfaces, glm::vec4(1.0f), !def.rt.emissive
); );
} }
auto& points = def->modelExtraPoints; auto& points = def.modelExtraPoints;
glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f); glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f);
for (size_t i = 0; i < def->modelExtraPoints.size() / 4; i++) { for (size_t i = 0; i < def.modelExtraPoints.size() / 4; i++) {
const UVRegion& reg = def->modelUVs[def->modelBoxes.size() * 6 + i]; const UVRegion& reg = def.modelUVs[def.modelBoxes.size() * 6 + i];
batch->point((points[i * 4 + 0] - poff) * pmul, glm::vec2(reg.u1, reg.v1), glm::vec4(1.0)); batch->point((points[i * 4 + 0] - poff) * pmul, glm::vec2(reg.u1, reg.v1), glm::vec4(1.0));
batch->point((points[i * 4 + 1] - poff) * pmul, glm::vec2(reg.u2, reg.v1), glm::vec4(1.0)); batch->point((points[i * 4 + 1] - poff) * pmul, glm::vec2(reg.u2, reg.v1), glm::vec4(1.0));
@ -154,9 +154,9 @@ std::unique_ptr<Atlas> BlocksPreview::build(
fbo.bind(); fbo.bind();
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
auto def = indices->blocks.get(i); //FIXME: Potentional null pointer auto& def = indices->blocks.require(i);
atlas->getTexture()->bind(); atlas->getTexture()->bind();
builder.add(def->name, draw(cache, shader, &fbo, &batch, def, iconSize)); //-V522 builder.add(def.name, draw(cache, shader, &fbo, &batch, def, iconSize));
} }
fbo.unbind(); fbo.unbind();

View File

@ -22,7 +22,7 @@ class BlocksPreview {
Shader* shader, Shader* shader,
Framebuffer* framebuffer, Framebuffer* framebuffer,
Batch3D* batch, Batch3D* batch,
const Block* block, const Block& block,
int size int size
); );
public: public:

View File

@ -142,11 +142,12 @@ void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality)
ctx.setFramebuffer(fbo.get()); ctx.setFramebuffer(fbo.get());
ctx.setViewport(Viewport(size, size)); ctx.setViewport(Viewport(size, size));
auto cubemap = dynamic_cast<Cubemap*>(fbo->getTexture()); //FIXME: Potentional null pointer auto cubemap = dynamic_cast<Cubemap*>(fbo->getTexture());
assert(cubemap != nullptr);
ready = true; ready = true;
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
cubemap->bind(); //-V522 cubemap->bind();
shader->use(); shader->use();
const glm::vec3 xaxs[] = { const glm::vec3 xaxs[] = {

View File

@ -121,9 +121,9 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
itemid_t itemid = bound->getItemId(); itemid_t itemid = bound->getItemId();
if (itemid != prevItem) { if (itemid != prevItem) {
if (itemid) { if (itemid) {
auto def = content->getIndices()->items.get(itemid); //FIXME: Potentional null pointer auto& def = content->getIndices()->items.require(itemid);
tooltip = util::pascal_case( tooltip = util::pascal_case(
langs::get(util::str2wstr_utf8(def->caption)) //-V522 langs::get(util::str2wstr_utf8(def.caption))
); );
} else { } else {
tooltip.clear(); tooltip.clear();
@ -159,7 +159,7 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
auto previews = assets->get<Atlas>("block-previews"); auto previews = assets->get<Atlas>("block-previews");
auto indices = content->getIndices(); auto indices = content->getIndices();
auto& item = indices->items.require(stack.getItemId()); //FIXME: Potentional null pointer auto& item = indices->items.require(stack.getItemId());
switch (item.iconType) { switch (item.iconType) {
case item_icon_type::none: case item_icon_type::none:
break; break;
@ -268,12 +268,13 @@ void SlotView::clicked(gui::GUI* gui, mousecode button) {
stack.setCount(halfremain); stack.setCount(halfremain);
} }
} else { } else {
auto stackDef = content->getIndices()->items.get(stack.getItemId()); //FIXME: Potentional null pointer auto& stackDef =
content->getIndices()->items.require(stack.getItemId());
if (stack.isEmpty()) { if (stack.isEmpty()) {
stack.set(grabbed); stack.set(grabbed);
stack.setCount(1); stack.setCount(1);
grabbed.setCount(grabbed.getCount() - 1); grabbed.setCount(grabbed.getCount() - 1);
} else if (stack.accepts(grabbed) && stack.getCount() < stackDef->stackSize){ //-V522 } else if (stack.accepts(grabbed) && stack.getCount() < stackDef.stackSize) {
stack.setCount(stack.getCount() + 1); stack.setCount(stack.getCount() + 1);
grabbed.setCount(grabbed.getCount() - 1); grabbed.setCount(grabbed.getCount() - 1);
} }

View File

@ -26,8 +26,8 @@ bool ItemStack::accepts(const ItemStack& other) const {
} }
void ItemStack::move(ItemStack& item, const ContentIndices* indices) { void ItemStack::move(ItemStack& item, const ContentIndices* indices) {
auto def = indices->items.get(item.getItemId()); //FIXME: Potentional null pointer auto& def = indices->items.require(item.getItemId());
int count = std::min(item.count, def->stackSize - this->count); //-V522 int count = std::min(item.count, def.stackSize - this->count);
if (isEmpty()) { if (isEmpty()) {
set(ItemStack(item.getItemId(), count)); set(ItemStack(item.getItemId(), count));
} else { } else {

View File

@ -153,8 +153,8 @@ int64_t BlocksController::createBlockInventory(int x, int y, int z) {
auto inv = chunk->getBlockInventory(lx, y, lz); auto inv = chunk->getBlockInventory(lx, y, lz);
if (inv == nullptr) { if (inv == nullptr) {
auto indices = level->content->getIndices(); auto indices = level->content->getIndices();
auto def = indices->blocks.get(chunk->voxels[vox_index(lx, y, lz)].id); //FIXME: Potentional null pointer auto& def = indices->blocks.require(chunk->voxels[vox_index(lx, y, lz)].id);
int invsize = def->inventorySize; //-V522 int invsize = def.inventorySize;
if (invsize == 0) { if (invsize == 0) {
return 0; return 0;
} }

View File

@ -335,8 +335,8 @@ static int determine_rotation(
static void pick_block( static void pick_block(
ContentIndices* indices, Chunks* chunks, Player* player, int x, int y, int z ContentIndices* indices, Chunks* chunks, Player* player, int x, int y, int z
) { ) {
auto block = indices->blocks.get(chunks->get(x, y, z)->id); //FIXME: Potentional null pointer auto& block = indices->blocks.require(chunks->get(x, y, z)->id);
itemid_t id = block->rt.pickingItem; //-V522 itemid_t id = block.rt.pickingItem;
auto inventory = player->getInventory(); auto inventory = player->getInventory();
size_t slotid = inventory->findSlotByItem(id, 0, 10); size_t slotid = inventory->findSlotByItem(id, 0, 10);
if (slotid == Inventory::npos) { if (slotid == Inventory::npos) {

View File

@ -119,11 +119,11 @@ static int l_get_x(lua::State* L) {
if (vox == nullptr) { if (vox == nullptr) {
return lua::pushivec3_stack(L, 1, 0, 0); return lua::pushivec3_stack(L, 1, 0, 0);
} }
auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer auto& def = level->content->getIndices()->blocks.require(vox->id);
if (!def->rotatable) { //-V522 if (!def.rotatable) {
return lua::pushivec3_stack(L, 1, 0, 0); return lua::pushivec3_stack(L, 1, 0, 0);
} else { } else {
const CoordSystem& rot = def->rotations.variants[vox->state.rotation]; const CoordSystem& rot = def.rotations.variants[vox->state.rotation];
return lua::pushivec3_stack(L, rot.axisX.x, rot.axisX.y, rot.axisX.z); return lua::pushivec3_stack(L, rot.axisX.x, rot.axisX.y, rot.axisX.z);
} }
} }
@ -136,11 +136,11 @@ static int l_get_y(lua::State* L) {
if (vox == nullptr) { if (vox == nullptr) {
return lua::pushivec3_stack(L, 0, 1, 0); return lua::pushivec3_stack(L, 0, 1, 0);
} }
auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer auto& def = level->content->getIndices()->blocks.require(vox->id);
if (!def->rotatable) { //-V522 if (!def.rotatable) {
return lua::pushivec3_stack(L, 0, 1, 0); return lua::pushivec3_stack(L, 0, 1, 0);
} else { } else {
const CoordSystem& rot = def->rotations.variants[vox->state.rotation]; const CoordSystem& rot = def.rotations.variants[vox->state.rotation];
return lua::pushivec3_stack(L, rot.axisY.x, rot.axisY.y, rot.axisY.z); return lua::pushivec3_stack(L, rot.axisY.x, rot.axisY.y, rot.axisY.z);
} }
} }
@ -153,11 +153,11 @@ static int l_get_z(lua::State* L) {
if (vox == nullptr) { if (vox == nullptr) {
return lua::pushivec3_stack(L, 0, 0, 1); return lua::pushivec3_stack(L, 0, 0, 1);
} }
auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer auto& def = level->content->getIndices()->blocks.require(vox->id);
if (!def->rotatable) { //-V522 if (!def.rotatable) {
return lua::pushivec3_stack(L, 0, 0, 1); return lua::pushivec3_stack(L, 0, 0, 1);
} else { } else {
const CoordSystem& rot = def->rotations.variants[vox->state.rotation]; const CoordSystem& rot = def.rotations.variants[vox->state.rotation];
return lua::pushivec3_stack(L, rot.axisZ.x, rot.axisZ.y, rot.axisZ.z); return lua::pushivec3_stack(L, rot.axisZ.x, rot.axisZ.y, rot.axisZ.z);
} }
} }

View File

@ -53,34 +53,40 @@ static DocumentNode getDocumentNode(lua::State* L, int idx = 1) {
static int l_menu_back(lua::State* L) { static int l_menu_back(lua::State* L) {
auto node = getDocumentNode(L); auto node = getDocumentNode(L);
auto menu = dynamic_cast<Menu*>(node.node.get()); //FIXME: Potentional null pointer if (auto menu = dynamic_cast<Menu*>(node.node.get())) {
menu->back(); //-V522 menu->back();
}
return 0; return 0;
} }
static int l_menu_reset(lua::State* L) { static int l_menu_reset(lua::State* L) {
auto node = getDocumentNode(L); auto node = getDocumentNode(L);
auto menu = dynamic_cast<Menu*>(node.node.get()); //FIXME: Potentional null pointer if (auto menu = dynamic_cast<Menu*>(node.node.get())) {
menu->reset(); //-V522 menu->reset();
}
return 0; return 0;
} }
static int l_textbox_paste(lua::State* L) { static int l_textbox_paste(lua::State* L) {
auto node = getDocumentNode(L); auto node = getDocumentNode(L);
auto box = dynamic_cast<TextBox*>(node.node.get()); //FIXME: Potentional null pointer if (auto box = dynamic_cast<TextBox*>(node.node.get())) {
auto text = lua::require_string(L, 2); auto text = lua::require_string(L, 2);
box->paste(util::str2wstr_utf8(text)); //-V522 box->paste(util::str2wstr_utf8(text));
}
return 0; return 0;
} }
static int l_container_add(lua::State* L) { static int l_container_add(lua::State* L) {
auto docnode = getDocumentNode(L); auto docnode = getDocumentNode(L);
auto node = dynamic_cast<Container*>(docnode.node.get()); //FIXME: Potentional null pointer auto node = dynamic_cast<Container*>(docnode.node.get());
if (node == nullptr) {
return 0;
}
auto xmlsrc = lua::require_string(L, 2); auto xmlsrc = lua::require_string(L, 2);
try { try {
auto subnode = auto subnode =
guiutil::create(xmlsrc, docnode.document->getEnvironment()); guiutil::create(xmlsrc, docnode.document->getEnvironment());
node->add(subnode); //-V522 node->add(subnode);
UINode::getIndices(subnode, docnode.document->getMapWriteable()); UINode::getIndices(subnode, docnode.document->getMapWriteable());
} catch (const std::exception& err) { } catch (const std::exception& err) {
throw std::runtime_error(err.what()); throw std::runtime_error(err.what());

View File

@ -49,11 +49,11 @@ static int l_hud_open_block(lua::State* L) {
std::to_string(y) + " " + std::to_string(z) std::to_string(y) + " " + std::to_string(z)
); );
} }
auto def = content->getIndices()->blocks.get(vox->id); auto& def = content->getIndices()->blocks.require(vox->id);
auto assets = engine->getAssets(); auto assets = engine->getAssets();
auto layout = assets->get<UiDocument>(def->uiLayout);//FIXME: Potentional null pointer //-V522 auto layout = assets->get<UiDocument>(def.uiLayout);
if (layout == nullptr) { if (layout == nullptr) {
throw std::runtime_error("block '" + def->name + "' has no ui layout"); throw std::runtime_error("block '" + def.name + "' has no ui layout");
} }
auto id = blocks->createBlockInventory(x, y, z); auto id = blocks->createBlockInventory(x, y, z);
@ -65,7 +65,7 @@ static int l_hud_open_block(lua::State* L) {
); );
lua::pushinteger(L, id); lua::pushinteger(L, id);
lua::pushstring(L, def->uiLayout); lua::pushstring(L, def.uiLayout);
return 2; return 2;
} }