From c82a6d05b0be9cdd5e16420ff06bd28190508ea7 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 10 Mar 2023 08:43:12 +0100 Subject: [PATCH 1/4] Code cleanup --- code/CApi/CInterfaceIOWrapper.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/code/CApi/CInterfaceIOWrapper.cpp b/code/CApi/CInterfaceIOWrapper.cpp index 579545ecc..f0e46cd08 100644 --- a/code/CApi/CInterfaceIOWrapper.cpp +++ b/code/CApi/CInterfaceIOWrapper.cpp @@ -5,8 +5,6 @@ Open Asset Import Library (assimp) Copyright (c) 2006-2022, assimp team - - All rights reserved. Redistribution and use of this software in source and binary forms, @@ -47,14 +45,16 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace Assimp { +// ------------------------------------------------------------------------------------------------ CIOStreamWrapper::~CIOStreamWrapper() { - /* Various places depend on this destructor to close the file */ - if (mFile) { + // Various places depend on this destructor to close the file + if (mFile != nullptr) { + mIO->mFileSystem->CloseProc(mIO->mFileSystem, mFile); } } -// ................................................................... +// ------------------------------------------------------------------------------------------------ size_t CIOStreamWrapper::Read(void *pvBuffer, size_t pSize, size_t pCount) { @@ -62,7 +62,7 @@ size_t CIOStreamWrapper::Read(void *pvBuffer, return mFile->ReadProc(mFile, (char *)pvBuffer, pSize, pCount); } -// ................................................................... +// ------------------------------------------------------------------------------------------------ size_t CIOStreamWrapper::Write(const void *pvBuffer, size_t pSize, size_t pCount) { @@ -70,23 +70,23 @@ size_t CIOStreamWrapper::Write(const void *pvBuffer, return mFile->WriteProc(mFile, (const char *)pvBuffer, pSize, pCount); } -// ................................................................... +// ------------------------------------------------------------------------------------------------ aiReturn CIOStreamWrapper::Seek(size_t pOffset, aiOrigin pOrigin) { return mFile->SeekProc(mFile, pOffset, pOrigin); } -// ................................................................... +// ------------------------------------------------------------------------------------------------ size_t CIOStreamWrapper::Tell() const { return mFile->TellProc(mFile); } -// ................................................................... +// ------------------------------------------------------------------------------------------------ size_t CIOStreamWrapper::FileSize() const { return mFile->FileSizeProc(mFile); } -// ................................................................... +// ------------------------------------------------------------------------------------------------ void CIOStreamWrapper::Flush() { return mFile->FlushProc(mFile); } From 63dae0a7f24c0bc965f936e5b3b96b8d83a93e5f Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 10 Mar 2023 08:48:11 +0100 Subject: [PATCH 2/4] Fix: Add security asserts. --- code/CApi/CInterfaceIOWrapper.h | 55 ++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/code/CApi/CInterfaceIOWrapper.h b/code/CApi/CInterfaceIOWrapper.h index 768be3746..28d4c3e75 100644 --- a/code/CApi/CInterfaceIOWrapper.h +++ b/code/CApi/CInterfaceIOWrapper.h @@ -47,48 +47,59 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include namespace Assimp { class CIOSystemWrapper; // ------------------------------------------------------------------------------------------------ -// Custom IOStream implementation for the C-API -class CIOStreamWrapper : public IOStream { +/// @brief Custom IOStream implementation for the C-API- +// ------------------------------------------------------------------------------------------------ +class CIOStreamWrapper final : public IOStream { public: - explicit CIOStreamWrapper(aiFile *pFile, CIOSystemWrapper *io) : - mFile(pFile), - mIO(io) {} - ~CIOStreamWrapper(void); - - size_t Read(void *pvBuffer, size_t pSize, size_t pCount); - size_t Write(const void *pvBuffer, size_t pSize, size_t pCount); - aiReturn Seek(size_t pOffset, aiOrigin pOrigin); - size_t Tell(void) const; - size_t FileSize() const; - void Flush(); + explicit CIOStreamWrapper(aiFile *pFile, CIOSystemWrapper *io); + ~CIOStreamWrapper() override; + size_t Read(void *pvBuffer, size_t pSize, size_t pCount) override; + size_t Write(const void *pvBuffer, size_t pSize, size_t pCount) override; + aiReturn Seek(size_t pOffset, aiOrigin pOrigin) override; + size_t Tell(void) const override; + size_t FileSize() const override; + void Flush() override; private: aiFile *mFile; CIOSystemWrapper *mIO; }; -class CIOSystemWrapper : public IOSystem { +inline CIOStreamWrapper::CIOStreamWrapper(aiFile *pFile, CIOSystemWrapper *io) : + mFile(pFile), + mIO(io) { + ai_assert(io != nullptr); +} + +// ------------------------------------------------------------------------------------------------ +/// @brief Custom IO-System wrapper implementation for the C-API. +// ------------------------------------------------------------------------------------------------ +class CIOSystemWrapper final : public IOSystem { friend class CIOStreamWrapper; public: - explicit CIOSystemWrapper(aiFileIO *pFile) : - mFileSystem(pFile) {} - - bool Exists(const char *pFile) const; - char getOsSeparator() const; - IOStream *Open(const char *pFile, const char *pMode = "rb"); - void Close(IOStream *pFile); + explicit CIOSystemWrapper(aiFileIO *pFile); + ~CIOSystemWrapper() override = default; + bool Exists(const char *pFile) const override; + char getOsSeparator() const override; + IOStream *Open(const char *pFile, const char *pMode = "rb") override; + void Close(IOStream *pFile) override; private: aiFileIO *mFileSystem; }; +inline CIOSystemWrapper::CIOSystemWrapper(aiFileIO *pFile) : mFileSystem(pFile) { + ai_assert(pFile != nullptr); +} + } // namespace Assimp -#endif +#endif // AI_CIOSYSTEM_H_INCLUDED From 2f7882cb8d42e41d5330b154316da3d5617f5769 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 10 Mar 2023 08:49:58 +0100 Subject: [PATCH 3/4] Update: Small code cleanup --- code/CApi/AssimpCExport.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/code/CApi/AssimpCExport.cpp b/code/CApi/AssimpCExport.cpp index 5e43958d0..e3d85504d 100644 --- a/code/CApi/AssimpCExport.cpp +++ b/code/CApi/AssimpCExport.cpp @@ -5,8 +5,6 @@ Open Asset Import Library (assimp) Copyright (c) 2006-2022, assimp team - - All rights reserved. Redistribution and use of this software in source and binary forms, @@ -52,7 +50,7 @@ Assimp C export interface. See Exporter.cpp for some notes. #include #include -using namespace Assimp; +namespace Assimp { // ------------------------------------------------------------------------------------------------ ASSIMP_API size_t aiGetExportFormatCount(void) { @@ -141,4 +139,6 @@ ASSIMP_API C_STRUCT void aiReleaseExportBlob(const aiExportDataBlob *pData) { delete pData; } +} // namespace Assimp + #endif // !ASSIMP_BUILD_NO_EXPORT From ffd222334f647f8d65762343a9ebc4ff6247c272 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 10 Mar 2023 09:00:37 +0100 Subject: [PATCH 4/4] Fix: Remove buggy namespace declaration --- code/CApi/AssimpCExport.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/code/CApi/AssimpCExport.cpp b/code/CApi/AssimpCExport.cpp index e3d85504d..21e40205c 100644 --- a/code/CApi/AssimpCExport.cpp +++ b/code/CApi/AssimpCExport.cpp @@ -50,7 +50,7 @@ Assimp C export interface. See Exporter.cpp for some notes. #include #include -namespace Assimp { +using namespace Assimp; // ------------------------------------------------------------------------------------------------ ASSIMP_API size_t aiGetExportFormatCount(void) { @@ -139,6 +139,4 @@ ASSIMP_API C_STRUCT void aiReleaseExportBlob(const aiExportDataBlob *pData) { delete pData; } -} // namespace Assimp - #endif // !ASSIMP_BUILD_NO_EXPORT