From 59901599941981c239b53b094a05e321efb06ad5 Mon Sep 17 00:00:00 2001 From: "Seungho, Baek" Date: Thu, 20 Sep 2018 13:29:17 +0900 Subject: [PATCH] [Tizen] Path length check and coverity issue fix in Scene - Path has to have length under 256 character. - Coverity issue fix Change-Id: I1fd185e4b1174201736d38bf8751ec9f806cd04b Signed-off-by: Seungho, Baek --- .../internal/controls/scene/gltf-loader.cpp | 63 ++++++++++++++-------- .../internal/controls/scene/scene-impl.cpp | 5 ++ 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/dali-toolkit/internal/controls/scene/gltf-loader.cpp b/dali-toolkit/internal/controls/scene/gltf-loader.cpp index f376939..ebfdae4 100644 --- a/dali-toolkit/internal/controls/scene/gltf-loader.cpp +++ b/dali-toolkit/internal/controls/scene/gltf-loader.cpp @@ -14,6 +14,7 @@ * limitations under the License. * */ + // CLASS HEADER #include #include @@ -38,6 +39,14 @@ namespace Toolkit namespace Internal { +namespace +{ + +// Maximum path length of linux. +const unsigned int MAX_PATH_LENGTH = 4096; + +}//namespace + GltfLoader::GltfLoader() : mNodes( NULL ), mRoot( NULL ) @@ -63,25 +72,22 @@ bool GltfLoader::LoadScene( const std::string& filePath, Internal::Scene& scene } mRoot = mParser.GetRoot(); - if( !mRoot ) - { - return false; - } - - if( !LoadAssets() ) + if( mRoot && + LoadAssets() && + CreateScene( scene ) ) { - return false; - } - - if( !CreateScene( scene ) ) - { - return false; + return true; } - return true; + return false; } bool GltfLoader::ParseGltf( const std::string& filePath ) { + if( filePath.length() > MAX_PATH_LENGTH ) + { + DALI_LOG_ERROR( "File path is too long.\n" ); + return false; + } std::ifstream fileStream( filePath.c_str() ); std::string fileBuffer( ( std::istreambuf_iterator( fileStream ) ), ( std::istreambuf_iterator() ) ); @@ -92,10 +98,10 @@ bool GltfLoader::ParseGltf( const std::string& filePath ) bool GltfLoader::LoadAssets() { - if( LoadBinaryData( mRoot ) && // pass a reference - LoadTextureArray( mRoot ) && // pass a reference - LoadMaterialSetArray( mRoot ) && // pass a reference - LoadMeshArray( mRoot ) // pass a reference + if( LoadBinaryData( mRoot ) && + LoadTextureArray( mRoot ) && + LoadMaterialSetArray( mRoot ) && + LoadMeshArray( mRoot ) ) { return true; @@ -355,13 +361,17 @@ bool GltfLoader::LoadTextureArray( const TreeNode* root ) Texture GltfLoader::LoadTexture( const char* imageUrl, bool generateMipmaps ) { Texture texture; + if( std::string( imageUrl ).length() > MAX_PATH_LENGTH ) + { + DALI_LOG_ERROR( "Image path is too long.\n" ); + return texture; + } Devel::PixelBuffer pixelBuffer = LoadImageFromFile( imageUrl ); if( pixelBuffer ) { texture = Texture::New( TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight() ); PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer ); texture.Upload( pixelData ); - if( generateMipmaps ) { texture.GenerateMipmaps(); @@ -1115,7 +1125,6 @@ Actor GltfLoader::AddNode( Scene& scene, int index ) if( ( tempNode = node->GetChild( "mesh" ) ) ) { MeshInfo meshInfo = mMeshArray[tempNode->GetInteger()]; - GLTF::MaterialInfo materialInfo = mMaterialArray[meshInfo.materialsIdx]; bool isMaterial = ( meshInfo.materialsIdx >= 0 ); TextureSet textureSet; @@ -1138,6 +1147,7 @@ Actor GltfLoader::AddNode( Scene& scene, int index ) bool useIBL = ( scene.GetLightType() >= Toolkit::Scene::LightType::IMAGE_BASED_LIGHT ); if( isMaterial ) { + GLTF::MaterialInfo materialInfo = mMaterialArray[meshInfo.materialsIdx]; if( SetTextureAndSampler( textureSet, materialInfo.baseColorTexture.index, FRAGMENT_SHADER, DEFINE_BASECOLOR_TEXTURE, addIdx ) ) { shaderTypeIndex += static_cast( ShaderType::BASECOLOR_SHADER ); @@ -1224,6 +1234,7 @@ Actor GltfLoader::AddNode( Scene& scene, int index ) actor.RegisterProperty( "uIsColor", meshInfo.attribute.COLOR.size() > 0 ); if( isMaterial ) { + GLTF::MaterialInfo materialInfo = mMaterialArray[meshInfo.materialsIdx]; actor.RegisterProperty( "uBaseColorFactor", materialInfo.baseColorFactor ); actor.RegisterProperty( "uMetallicRoughnessFactors", Vector2( materialInfo.metallicFactor, materialInfo.roughnessFactor ) ); @@ -1503,14 +1514,22 @@ bool GltfLoader::LoadAnimationSamplers( const TreeNode& animation, AnimationInfo template bool GltfLoader::ReadBinFile( Vector &dataBuffer, std::string url, int offset, int count ) { + if( url.length() > MAX_PATH_LENGTH ) + { + DALI_LOG_ERROR( "Binary file path is too long.\n" ); + return false; + } dataBuffer.Resize( count ); FILE* fp = fopen( url.c_str(), "rb" ); - if( NULL == fp ) + if( fp == NULL ) { return false; } - fseek( fp, offset, SEEK_SET ); - ssize_t result = fread( &dataBuffer[0], sizeof( T ), count, fp ); + ssize_t result = -1; + if( !fseek( fp, offset, SEEK_SET ) ) + { + result = fread( &dataBuffer[0], sizeof( T ), count, fp ); + } fclose( fp ); return ( result >= 0 ); diff --git a/dali-toolkit/internal/controls/scene/scene-impl.cpp b/dali-toolkit/internal/controls/scene/scene-impl.cpp index 2c70685..c09f21c 100644 --- a/dali-toolkit/internal/controls/scene/scene-impl.cpp +++ b/dali-toolkit/internal/controls/scene/scene-impl.cpp @@ -14,6 +14,7 @@ * limitations under the License. * */ + // CLASS HEADER #include "scene-impl.h" @@ -177,6 +178,10 @@ void Scene::UploadTextureFace( Texture& texture, Devel::PixelBuffer pixelBuffer, { faceSize = imageWidth / 6; } + else + { + return; + } unsigned int xOffset = cubeMap_index_x[cubeType][faceIndex] * faceSize; unsigned int yOffset = cubeMap_index_y[cubeType][faceIndex] * faceSize; -- 2.7.4