From fa66f28d52c74e1d96b45832baab0506456a01b6 Mon Sep 17 00:00:00 2001 From: Marcus Klang Date: Thu, 19 Sep 2019 11:45:55 +0200 Subject: [PATCH] Fixed getTextureData() when failed to load texture getTextureData() created an image based on width and height values that are undefined if image failed to load. Replaced with default image and image is constructed only if successfully loaded. --- src/core/helpers.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/core/helpers.cpp b/src/core/helpers.cpp index 9326f086..cca1c802 100644 --- a/src/core/helpers.cpp +++ b/src/core/helpers.cpp @@ -49,11 +49,16 @@ getTextureData(std::string const& filename, u32& width, u32& height, bool flip) auto const path = config::resources_path(filename); auto const channels_nb = 4u; unsigned char* image_data = stbi_load(path.c_str(), reinterpret_cast(&width), reinterpret_cast(&height), nullptr, channels_nb); - std::vector image(width * height * channels_nb); if (image_data == nullptr) { LogWarning("Couldn't load or decode image file %s", path.c_str()); - return image; + + // Provide a small empty image instead in case of failure. + width = 16; + height = 16; + return std::vector(width * height * channels_nb); } + + std::vector image(width * height * channels_nb); if (!flip) { std::memcpy(image.data(), image_data, image.size()); stbi_image_free(image_data);