From 121c0e7d0c9db2db6c80897b46dfc4743f64d2b7 Mon Sep 17 00:00:00 2001 From: Hill Ma Date: Mon, 7 Jun 2021 21:53:28 -0700 Subject: [PATCH 1/2] Add GetEmbeddedTextureAndIndex() to aiScene. It allows the caller to get the index of the embedded texture that is always computed anyway. --- include/assimp/scene.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/include/assimp/scene.h b/include/assimp/scene.h index 769499a27..fb3b2ef6e 100644 --- a/include/assimp/scene.h +++ b/include/assimp/scene.h @@ -397,22 +397,27 @@ struct aiScene //! Returns an embedded texture const aiTexture* GetEmbeddedTexture(const char* filename) const { + return GetEmbeddedTextureAndIndex(filename).first; + } + + //! Returns an embedded texture and its index + std::pair GetEmbeddedTextureAndIndex(const char* filename) const { // lookup using texture ID (if referenced like: "*1", "*2", etc.) if ('*' == *filename) { int index = std::atoi(filename + 1); if (0 > index || mNumTextures <= static_cast(index)) - return nullptr; - return mTextures[index]; + return std::make_pair(nullptr, -1); + return std::make_pair(mTextures[index], index); } // lookup using filename const char* shortFilename = GetShortFilename(filename); for (unsigned int i = 0; i < mNumTextures; i++) { const char* shortTextureFilename = GetShortFilename(mTextures[i]->mFilename.C_Str()); if (strcmp(shortTextureFilename, shortFilename) == 0) { - return mTextures[i]; + return std::make_pair(mTextures[i], i); } } - return nullptr; + return std::make_pair(nullptr, -1); } #endif // __cplusplus From a8c75c34a1dd297dfed74e660d30b83d4acc9a56 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Thu, 17 Jun 2021 21:31:28 +0200 Subject: [PATCH 2/2] Update scene.h Add some more checks against nullptr dereferecnes. --- include/assimp/scene.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/include/assimp/scene.h b/include/assimp/scene.h index fb3b2ef6e..522ddc6dc 100644 --- a/include/assimp/scene.h +++ b/include/assimp/scene.h @@ -5,8 +5,6 @@ Open Asset Import Library (assimp) Copyright (c) 2006-2021, assimp team - - All rights reserved. Redistribution and use of this software in source and binary forms, @@ -402,15 +400,23 @@ struct aiScene //! Returns an embedded texture and its index std::pair GetEmbeddedTextureAndIndex(const char* filename) const { + if(nullptr==filename) { + return std::make_pair(nullptr, -1); + } // lookup using texture ID (if referenced like: "*1", "*2", etc.) if ('*' == *filename) { int index = std::atoi(filename + 1); - if (0 > index || mNumTextures <= static_cast(index)) + if (0 > index || mNumTextures <= static_cast(index)) { return std::make_pair(nullptr, -1); + } return std::make_pair(mTextures[index], index); } // lookup using filename const char* shortFilename = GetShortFilename(filename); + if (nullptr == shortFilename) { + return std::make_pair(nullptr, -1); + } + for (unsigned int i = 0; i < mNumTextures; i++) { const char* shortTextureFilename = GetShortFilename(mTextures[i]->mFilename.C_Str()); if (strcmp(shortTextureFilename, shortFilename) == 0) {