17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-01 10:22:09 +01:00

fix(sonar): addressing reliability/security issues

This commit is contained in:
jerrrrycho
2026-10-03 20:21:28 +02:00
parent 51521a135a
commit d88fa5d8e2
32 changed files with 189 additions and 162 deletions

View File

@@ -1,22 +1,43 @@
#pragma once
#include <limits>
#include <mutex>
#include "model/Texture.h"
#include "rendering/renderer.h"
class deferred_image {
public:
// value returned by get() when the image isn't (yet) available
static constexpr GLuint invalid_id { std::numeric_limits<GLuint>::max() };
deferred_image() = default;
deferred_image(const std::string &p) : path(p) { }
deferred_image(const deferred_image&) = delete;
deferred_image(deferred_image&&) = default;
deferred_image &operator=(deferred_image&&) = default;
deferred_image(deferred_image &&other) noexcept
{
std::lock_guard<std::mutex> lock(other.mutex);
path = std::move(other.path);
image = other.image;
}
deferred_image &operator=(deferred_image &&other) noexcept
{
if (this != &other) {
std::scoped_lock lock(mutex, other.mutex);
path = std::move(other.path);
image = other.image;
}
return *this;
}
operator bool() const
{
std::lock_guard<std::mutex> lock(mutex);
return image != null_handle || !path.empty();
}
GLuint get() const
{
std::lock_guard<std::mutex> lock(mutex);
if (!path.empty()) {
image = GfxRenderer->Fetch_Texture(path, true);
path.clear();
@@ -30,11 +51,12 @@ public:
return tex.get_id();
}
return -1;
return invalid_id;
}
glm::ivec2 size() const
{
std::lock_guard<std::mutex> lock(mutex);
if (image != null_handle) {
auto &tex = GfxRenderer->Texture(image);
return glm::ivec2(tex.get_width(), tex.get_height());
@@ -43,6 +65,8 @@ public:
}
private:
// guards lazy loading performed by the const accessors
mutable std::mutex mutex;
mutable std::string path;
mutable texture_handle image = 0;
};

View File

@@ -281,7 +281,7 @@ void ui::scenerylist_panel::draw_trainset(trainset_desc &trainset)
for (auto &dyn_desc : trainset.vehicles) {
deferred_image *mini = nullptr;
if (dyn_desc.skin && dyn_desc.skin->mini.get() != (GLuint)-1)
if (dyn_desc.skin && dyn_desc.skin->mini.get() != deferred_image::invalid_id)
mini = &dyn_desc.skin->mini;
else
mini = &placeholder_mini;

View File

@@ -270,7 +270,7 @@ bool ui::vehiclepicker_panel::selectable_image(const char *desc, bool selected,
}
GLuint tex = image->get();
if (tex != (GLuint)-1) {
if (tex != deferred_image::invalid_id) {
glm::ivec2 size = image->size();
float width = 30.0f / size.y * size.x;
ImGui::SameLine(ImGui::GetContentRegionAvail().x - width);