fix buffer overflow with arrays

main
Brett 2024-09-17 01:07:27 -04:00
parent 3f94d6333f
commit 36159a33b8
7 changed files with 226 additions and 10 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.25)
set(BLT_GRAPHICS_VERSION 0.13.31)
set(BLT_GRAPHICS_VERSION 0.13.32)
set(BLT_GRAPHICS_TEST_VERSION 0.0.1)
project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION})

View File

@ -47,22 +47,27 @@ namespace blt::gfx
BLT_MAKE_GETTER_AND_SETTER(blt::i32, desired_width);
BLT_MAKE_GETTER_AND_SETTER(blt::i32, desired_height);
BLT_MAKE_GETTER_AND_SETTER(blt::i32, desired_channels);
private:
std::string path;
std::string name;
blt::i32 desired_width = 0;
blt::i32 desired_height = 0;
blt::i32 desired_channels = 0;
};
class texture_array
{
public:
texture_array(std::string name, i32 width, i32 height): name(std::move(name)), width(width), height(height)
texture_array(std::string name, i32 width, i32 height, blt::i32 channels = 4):
name(std::move(name)), width(width), height(height), channels(channels)
{}
void add_texture(std::string path, std::string t_name = "")
{
textures.push_back(texture_info{std::move(path), std::move(t_name)}.set_desired_width(width).set_desired_height(height));
textures.push_back(texture_info{std::move(path), std::move(t_name)}.set_desired_width(width).set_desired_height(height)
.set_desired_channels(channels));
}
BLT_MAKE_GETTER(std::vector<texture_info>, textures);
@ -73,6 +78,7 @@ namespace blt::gfx
std::string name;
i32 width;
i32 height;
i32 channels;
};
class resource_manager
@ -93,8 +99,9 @@ namespace blt::gfx
void add(texture_file* file) const
{
// if order is present the vec is assumed to be resized. otherwise assume order doesn't matter and push to an empty vec
if (order)
vec->insert(vec->begin() + static_cast<blt::ptrdiff_t>(*order), file);
(*vec)[*order] = file;
else
vec->push_back(file);
}
@ -110,6 +117,7 @@ namespace blt::gfx
std::string resource_prefix;
void load_to_gl();
public:
resource_manager() = default;
@ -133,6 +141,8 @@ namespace blt::gfx
{
loaded_arrays.emplace_back(array.get_name(), std::vector<texture_file*>{});
auto& dest = loaded_arrays.back();
dest.second.reserve(array.get_textures().size());
dest.second.resize(array.get_textures().size());
for (const auto& [index, texture] : blt::enumerate(array.get_textures()))
textures_to_load.emplace_back(texture_destination{&dest.second, index}, std::move(texture));
}

View File

@ -84,13 +84,14 @@ namespace blt::gfx
private:
std::string m_name;
std::string m_path;
blt::i32 desired_channels;
mutable texture_data m_texture;
public:
/**
* @param path path to the texture file
* @param name reference name for this texture. If empty the texture will use path as its identifier
*/
explicit texture_file(const std::string& path, const std::string& name = "");
explicit texture_file(const std::string& path, const std::string& name = "", blt::i32 desired_channels = 0);
texture_file& resize(int target_width, int target_height);

@ -1 +1 @@
Subproject commit e3c925ed1186aa6924527b08671293b98be784c7
Subproject commit 5375231ce5e06ea69c71f73e0582383331f714e7

197
normal.txt Normal file

File diff suppressed because one or more lines are too long

View File

@ -27,6 +27,8 @@ namespace blt::gfx
{
for (const auto& p : textures_2d)
delete p.second;
for (auto& t : texture_arrays_2d)
delete t.second;
}
void resource_manager::load_resources(std::size_t threads)
@ -50,7 +52,7 @@ namespace blt::gfx
BLT_WARN("Texture '%s' does not exist on disk!", path.c_str());
return;
}
auto* file = new texture_file(path, texture.get_name());
auto* file = new texture_file(path, texture.get_name(), texture.get_desired_channels());
file->resize(texture.get_desired_width(), texture.get_desired_height());
{
std::scoped_lock lock(save_lock);
@ -104,6 +106,7 @@ namespace blt::gfx
auto gl = new gl_texture2D_array(reference_v->width(), reference_v->height(), static_cast<blt::i32>(back.second.size()));
for (auto [index, texture] : blt::enumerate(back.second))
{
BLT_TRACE(index);
gl->upload(texture->texture().data(), static_cast<blt::i32>(index));
delete texture;
}
@ -112,3 +115,4 @@ namespace blt::gfx
BLT_DEBUG("Loaded texture array '%s' of size %ld", back.first.c_str(), back.second.size());
}
}
}

View File

@ -109,14 +109,17 @@ blt::hashmap_t<GLint, format_t> make_format_table()
blt::hashmap_t<GLint, format_t> internal_to_texture = make_format_table();
blt::gfx::texture_file::texture_file(const std::string& path, const std::string& name): m_name(name.empty() ? path : name), m_path(path)
blt::gfx::texture_file::texture_file(const std::string& path, const std::string& name, blt::i32 desired_channels):
m_name(name.empty() ? path : name), m_path(path), desired_channels(desired_channels)
{
stbi_set_flip_vertically_on_load(true);
m_texture.m_data = stbi_load(
m_path.c_str(),
&m_texture.m_width, &m_texture.m_height,
&m_texture.m_channels,
0);
desired_channels);
if (desired_channels != 0)
m_texture.m_channels = desired_channels;
}
blt::gfx::texture_file& blt::gfx::texture_file::resize(int target_width, int target_height)
@ -268,7 +271,8 @@ void blt::gfx::texture_gl2D_multisample::resize(int width, int height)
glTexStorage2DMultisample(textureBindType, samples, textureColorMode, width, height, GL_TRUE);
}
blt::gfx::gl_texture2D_array::gl_texture2D_array(int width, int height, int layers, GLint colorMode): texture_gl(width, height, layers, colorMode)
blt::gfx::gl_texture2D_array::gl_texture2D_array(int width, int height, int layers, GLint colorMode):
texture_gl(width, height, GL_TEXTURE_2D_ARRAY, colorMode), m_layers(layers)
{
bind();
setDefaults(GL_LINEAR);