From a038288af9c6dcb9ca433bb40a7994156a4481f9 Mon Sep 17 00:00:00 2001
From: jonathanklein <jonathanklein@67173fc5-114c-0410-ac8e-9d2fd5bffc1f>
Date: Sun, 12 Aug 2012 17:00:53 +0000
Subject: [PATCH] Ogre: Fixed memoryleak and missing \0 at end of cstring

git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@1298 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
---
 code/OgreImporter.cpp | 24 ++++++++++++------------
 code/OgreMaterial.cpp | 18 ++++++++++++++----
 2 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/code/OgreImporter.cpp b/code/OgreImporter.cpp
index c16cfc0f0..72141e437 100644
--- a/code/OgreImporter.cpp
+++ b/code/OgreImporter.cpp
@@ -100,7 +100,7 @@ void OgreImporter::InternReadFile(const std::string &pFile, aiScene *pScene, Ass
 
 	//Read the Mesh File:
 	boost::scoped_ptr<CIrrXML_IOStreamReader> mIOWrapper( new CIrrXML_IOStreamReader( file.get()));
-	XmlReader* MeshFile = irr::io::createIrrXMLReader(mIOWrapper.get());
+	boost::scoped_ptr<XmlReader> MeshFile(irr::io::createIrrXMLReader(mIOWrapper.get()));
 	if(!MeshFile)//parse the xml file
 		throw DeadlyImportError("Failed to create XML Reader for "+pFile);
 
@@ -108,21 +108,21 @@ void OgreImporter::InternReadFile(const std::string &pFile, aiScene *pScene, Ass
 	DefaultLogger::get()->debug("Mesh File opened");
 	
 	//Read root Node:
-	if(!(XmlRead(MeshFile) && string(MeshFile->getNodeName())=="mesh"))
+	if(!(XmlRead(MeshFile.get()) && string(MeshFile->getNodeName())=="mesh"))
 	{
 		throw DeadlyImportError("Root Node is not <mesh>! "+pFile+"  "+MeshFile->getNodeName());
 	}
 
 	//eventually load shared geometry
-	XmlRead(MeshFile);//shared geometry is optional, so we need a reed for the next two if's
+	XmlRead(MeshFile.get());//shared geometry is optional, so we need a reed for the next two if's
 	if(MeshFile->getNodeName()==string("sharedgeometry"))
 	{
-		unsigned int NumVertices=GetAttribute<int>(MeshFile, "vertexcount");;
+		unsigned int NumVertices=GetAttribute<int>(MeshFile.get(), "vertexcount");;
 
-		XmlRead(MeshFile);
+		XmlRead(MeshFile.get());
 		while(MeshFile->getNodeName()==string("vertexbuffer"))
 		{
-			ReadVertexBuffer(m_SharedGeometry, MeshFile, NumVertices);
+			ReadVertexBuffer(m_SharedGeometry, MeshFile.get(), NumVertices);
 		}
 	}
 
@@ -136,13 +136,13 @@ void OgreImporter::InternReadFile(const std::string &pFile, aiScene *pScene, Ass
 	//-------------------Read the submeshs and materials:-----------------------
 	std::list<boost::shared_ptr<SubMesh> > SubMeshes;
 	vector<aiMaterial*> Materials;
-	XmlRead(MeshFile);
+	XmlRead(MeshFile.get());
 	while(MeshFile->getNodeName()==string("submesh"))
 	{
 		SubMesh* theSubMesh=new SubMesh();
-		theSubMesh->MaterialName=GetAttribute<string>(MeshFile, "material");
+		theSubMesh->MaterialName=GetAttribute<string>(MeshFile.get(), "material");
 		DefaultLogger::get()->debug("Loading Submehs with Material: "+theSubMesh->MaterialName);
-		ReadSubMesh(*theSubMesh, MeshFile);
+		ReadSubMesh(*theSubMesh, MeshFile.get());
 
 		//just a index in a array, we add a mesh in each loop cycle, so we get indicies like 0, 1, 2 ... n;
 		//so it is important to do this before pushing the mesh in the vector!
@@ -170,9 +170,9 @@ void OgreImporter::InternReadFile(const std::string &pFile, aiScene *pScene, Ass
 	vector<Animation> Animations;
 	if(MeshFile->getNodeName()==string("skeletonlink"))
 	{
-		string SkeletonFile=GetAttribute<string>(MeshFile, "name");
+		string SkeletonFile=GetAttribute<string>(MeshFile.get(), "name");
 		LoadSkeleton(SkeletonFile, Bones, Animations);
-		XmlRead(MeshFile);
+		XmlRead(MeshFile.get());
 	}
 	else
 	{
@@ -185,7 +185,7 @@ void OgreImporter::InternReadFile(const std::string &pFile, aiScene *pScene, Ass
 	//now there might be boneassignments for the shared geometry:
 	if(MeshFile->getNodeName()==string("boneassignments"))
 	{
-		ReadBoneWeights(m_SharedGeometry, MeshFile);
+		ReadBoneWeights(m_SharedGeometry, MeshFile.get());
 	}
 
 
diff --git a/code/OgreMaterial.cpp b/code/OgreMaterial.cpp
index 95ca1cf42..48ab75523 100644
--- a/code/OgreMaterial.cpp
+++ b/code/OgreMaterial.cpp
@@ -148,12 +148,22 @@ aiMaterial* OgreImporter::LoadMaterial(const std::string MaterialName) const
 				}
 			}
 		}
+		//Fill the stream
 		boost::scoped_ptr<IOStream> MaterialFile(MatFilePtr);
-		vector<char> FileData(MaterialFile->FileSize());
-		MaterialFile->Read(&FileData[0], MaterialFile->FileSize(), 1);
-		BaseImporter::ConvertToUTF8(FileData);
+		if(MaterialFile->FileSize()>0)
+		{
+			vector<char> FileData(MaterialFile->FileSize());
+			MaterialFile->Read(&FileData[0], MaterialFile->FileSize(), 1);
+			BaseImporter::ConvertToUTF8(FileData);
 
-		ss << &FileData[0];
+			FileData.push_back('\0');//terminate the string with zero, so that the ss can parse it correctly
+			ss << &FileData[0];
+		}
+		else
+		{
+			DefaultLogger::get()->warn("Material " + MaterialName + " seams to be empty");
+			return NULL;
+		}
 	}
 
 	//create the material