From bdcff35d5d9d52f9b4b3d228e3087e6c01effdae Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 12 Aug 2016 17:13:18 +0200 Subject: [PATCH 1/3] closes https://github.com/assimp/assimp/issues/901 --- code/DefaultIOStream.cpp | 7 +++++-- code/DefaultIOStream.h | 8 ++++---- test/unit/utDefaultIOStream.cpp | 18 +++++++++++++++--- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/code/DefaultIOStream.cpp b/code/DefaultIOStream.cpp index f1809d894..73bfb35c4 100644 --- a/code/DefaultIOStream.cpp +++ b/code/DefaultIOStream.cpp @@ -55,6 +55,7 @@ DefaultIOStream::~DefaultIOStream() { if (mFile) { ::fclose(mFile); + mFile = nullptr; } } @@ -111,7 +112,7 @@ size_t DefaultIOStream::FileSize() const if (SIZE_MAX == cachedSize) { - // Although fseek/ftell would allow us to reuse the exising file handle here, + // Although fseek/ftell would allow us to reuse the existing file handle here, // it is generally unsafe because: // - For binary streams, it is not technically well-defined // - For text files the results are meaningless @@ -125,12 +126,14 @@ size_t DefaultIOStream::FileSize() const if (0 != err) return 0; cachedSize = (size_t) (fileStat.st_size); -#else +#elif defined __gnu_linux__ struct stat fileStat; int err = stat(mFilename.c_str(), &fileStat ); if (0 != err) return 0; cachedSize = (size_t) (fileStat.st_size); +#else +# error "Unknown platform" #endif } return cachedSize; diff --git a/code/DefaultIOStream.h b/code/DefaultIOStream.h index 7d7450c6e..779858839 100644 --- a/code/DefaultIOStream.h +++ b/code/DefaultIOStream.h @@ -59,11 +59,11 @@ class ASSIMP_API DefaultIOStream : public IOStream { friend class DefaultIOSystem; #if __ANDROID__ -#if __ANDROID_API__ > 9 -#if defined(AI_CONFIG_ANDROID_JNI_ASSIMP_MANAGER_SUPPORT) +# if __ANDROID_API__ > 9 +# if defined(AI_CONFIG_ANDROID_JNI_ASSIMP_MANAGER_SUPPORT) friend class AndroidJNIIOSystem; -#endif // defined(AI_CONFIG_ANDROID_JNI_ASSIMP_MANAGER_SUPPORT) -#endif // __ANDROID_API__ > 9 +# endif // defined(AI_CONFIG_ANDROID_JNI_ASSIMP_MANAGER_SUPPORT) +# endif // __ANDROID_API__ > 9 #endif // __ANDROID__ protected: diff --git a/test/unit/utDefaultIOStream.cpp b/test/unit/utDefaultIOStream.cpp index a4461ea5f..7d3c07f20 100644 --- a/test/unit/utDefaultIOStream.cpp +++ b/test/unit/utDefaultIOStream.cpp @@ -48,7 +48,12 @@ class utDefaultIOStream : public ::testing::Test { class TestDefaultIOStream : public DefaultIOStream { public: TestDefaultIOStream() - : DefaultIOStream() { + : DefaultIOStream() { + // empty + } + + TestDefaultIOStream( FILE* pFile, const std::string &strFilename ) + : DefaultIOStream( pFile, strFilename ) { // empty } @@ -58,7 +63,14 @@ public: }; TEST_F( utDefaultIOStream, FileSizeTest ) { - TestDefaultIOStream myStream; + char buffer[ L_tmpnam ]; + tmpnam( buffer ); + std::FILE *fs( std::fopen( buffer, "w+" ) ); + size_t written( std::fwrite( buffer, 1, sizeof( char ) * L_tmpnam, fs ) ); + std::fflush( fs ); + + TestDefaultIOStream myStream( fs, buffer ); size_t size = myStream.FileSize(); - EXPECT_EQ( size, 0 ); + EXPECT_EQ( size, sizeof( char ) * L_tmpnam ); + remove( buffer ); } From 83b02ff41f07a656a526063d1caebc2eee913f5d Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 12 Aug 2016 18:14:26 +0200 Subject: [PATCH 2/3] DefaultIOStream: add missing detection for apple-based OS. --- code/DefaultIOStream.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/DefaultIOStream.cpp b/code/DefaultIOStream.cpp index 73bfb35c4..d10c303d8 100644 --- a/code/DefaultIOStream.cpp +++ b/code/DefaultIOStream.cpp @@ -126,7 +126,7 @@ size_t DefaultIOStream::FileSize() const if (0 != err) return 0; cachedSize = (size_t) (fileStat.st_size); -#elif defined __gnu_linux__ +#elif defined __gnu_linux__ || defined __APPLE__ || defined __MACH__ struct stat fileStat; int err = stat(mFilename.c_str(), &fileStat ); if (0 != err) From 0379675fcad819ad1ca027f485de420a37d40e7a Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 12 Aug 2016 18:47:37 +0200 Subject: [PATCH 3/3] Add type to deal with 64-bit filesizes on x86_64-apple-darwin15.5.0x86_64-apple-darwin15.5.0 --- code/DefaultIOStream.cpp | 9 +++++---- code/DefaultIOStream.h | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/code/DefaultIOStream.cpp b/code/DefaultIOStream.cpp index d10c303d8..e9da3ff70 100644 --- a/code/DefaultIOStream.cpp +++ b/code/DefaultIOStream.cpp @@ -110,7 +110,7 @@ size_t DefaultIOStream::FileSize() const return 0; } - if (SIZE_MAX == cachedSize) { + if (SIZE_MAX == mCachedSize ) { // Although fseek/ftell would allow us to reuse the existing file handle here, // it is generally unsafe because: @@ -125,18 +125,19 @@ size_t DefaultIOStream::FileSize() const int err = _stat64( mFilename.c_str(), &fileStat ); if (0 != err) return 0; - cachedSize = (size_t) (fileStat.st_size); + mCachedSize = (size_t) (fileStat.st_size); #elif defined __gnu_linux__ || defined __APPLE__ || defined __MACH__ struct stat fileStat; int err = stat(mFilename.c_str(), &fileStat ); if (0 != err) return 0; - cachedSize = (size_t) (fileStat.st_size); + const unsigned long long cachedSize = fileStat.st_size; + mCachedSize = static_cast< size_t >( cachedSize ); #else # error "Unknown platform" #endif } - return cachedSize; + return mCachedSize; } // ---------------------------------------------------------------------------------- diff --git a/code/DefaultIOStream.h b/code/DefaultIOStream.h index 779858839..83bd3a9d4 100644 --- a/code/DefaultIOStream.h +++ b/code/DefaultIOStream.h @@ -111,7 +111,7 @@ private: std::string mFilename; // Cached file size - mutable size_t cachedSize; + mutable size_t mCachedSize; }; @@ -119,7 +119,7 @@ private: inline DefaultIOStream::DefaultIOStream () : mFile (NULL), mFilename (""), - cachedSize (SIZE_MAX) + mCachedSize(SIZE_MAX) { // empty } @@ -130,7 +130,7 @@ inline DefaultIOStream::DefaultIOStream (FILE* pFile, const std::string &strFilename) : mFile(pFile), mFilename(strFilename), - cachedSize (SIZE_MAX) + mCachedSize(SIZE_MAX) { // empty }