Skip to content

Commit 5d45fdd

Browse files
committed
fix: prevent UB in the texture allocation
1 parent 4207b47 commit 5d45fdd

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/engine/core/resource.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ ResourceManager::FindTexture(const std::string_view path) {
5454

5555
void ResourceManager::CreateTexture(const std::string_view path) {
5656
std::shared_ptr<Texture> texture = std::make_shared<Texture>();
57-
texture->SetTexture(IMG_LoadTexture(m_renderer.GetSDLRenderer(),
58-
std::string(path).c_str()));
57+
SDL_Texture *sdlTex =
58+
IMG_LoadTexture(m_renderer.GetSDLRenderer(), std::string(path).c_str());
59+
if (sdlTex == nullptr) {
60+
// TODO: add logging here
61+
return;
62+
}
63+
texture->SetTexture(sdlTex);
5964
m_textureMap.emplace(path, std::move(texture));
6065
}
6166

src/engine/core/texture.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Texture::~Texture() {
99
}
1010
}
1111
void Texture::SetTexture(SDL_Texture *texture) {
12+
if (texture == nullptr) {
13+
// TODO: add logging here
14+
return;
15+
}
1216
m_texture = texture;
1317
m_width = m_texture->w;
1418
m_height = m_texture->h;

0 commit comments

Comments
 (0)