From 39191dc11eb4ceded9a9e53ea6a002b0f57b40ff Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 18 Oct 2012 17:59:19 +0000 Subject: [PATCH] - added some safety checks when loading models (in case we have some corrupt data) --- source/shared_lib/sources/graphics/model.cpp | 90 ++++++++++++++++---- 1 file changed, 75 insertions(+), 15 deletions(-) diff --git a/source/shared_lib/sources/graphics/model.cpp b/source/shared_lib/sources/graphics/model.cpp index 064e9380..bc6df7e1 100644 --- a/source/shared_lib/sources/graphics/model.cpp +++ b/source/shared_lib/sources/graphics/model.cpp @@ -81,10 +81,38 @@ Mesh::~Mesh() { } void Mesh::init() { - vertices= new Vec3f[frameCount*vertexCount]; - normals= new Vec3f[frameCount*vertexCount]; - texCoords= new Vec2f[vertexCount]; - indices= new uint32[indexCount]; + try { + vertices= new Vec3f[frameCount*vertexCount]; + } + catch(bad_alloc& ba) { + char szBuf[8096]=""; + snprintf(szBuf,8096,"Error on line: %d size: %d msg: %s\n",__LINE__,(frameCount*vertexCount),ba.what()); + throw megaglest_runtime_error(szBuf); + } + try { + normals= new Vec3f[frameCount*vertexCount]; + } + catch(bad_alloc& ba) { + char szBuf[8096]=""; + snprintf(szBuf,8096,"Error on line: %d size: %d msg: %s\n",__LINE__,(frameCount*vertexCount),ba.what()); + throw megaglest_runtime_error(szBuf); + } + try { + texCoords= new Vec2f[vertexCount]; + } + catch(bad_alloc& ba) { + char szBuf[8096]=""; + snprintf(szBuf,8096,"Error on line: %d size: %d msg: %s\n",__LINE__,vertexCount,ba.what()); + throw megaglest_runtime_error(szBuf); + } + try { + indices= new uint32[indexCount]; + } + catch(bad_alloc& ba) { + char szBuf[8096]=""; + snprintf(szBuf,8096,"Error on line: %d size: %d msg: %s\n",__LINE__,indexCount,ba.what()); + throw megaglest_runtime_error(szBuf); + } } void Mesh::end() { @@ -226,14 +254,14 @@ void Mesh::loadV2(int meshIndex, const string &dir, FILE *f, TextureManager *tex if(meshHeader.normalFrameCount != meshHeader.vertexFrameCount) { - char szBuf[4096]=""; - sprintf(szBuf,"Old v2 model: vertex frame count different from normal frame count [v = %d, n = %d] meshIndex = %d modelFile [%s]",meshHeader.vertexFrameCount,meshHeader.normalFrameCount,meshIndex,modelFile.c_str()); + char szBuf[8096]=""; + snprintf(szBuf,8096,"Old v2 model: vertex frame count different from normal frame count [v = %d, n = %d] meshIndex = %d modelFile [%s]",meshHeader.vertexFrameCount,meshHeader.normalFrameCount,meshIndex,modelFile.c_str()); throw megaglest_runtime_error(szBuf); } if(meshHeader.texCoordFrameCount != 1) { - char szBuf[4096]=""; - sprintf(szBuf,"Old v2 model: texture coord frame count is not 1 [t = %d] meshIndex = %d modelFile [%s]",meshHeader.texCoordFrameCount,meshIndex,modelFile.c_str()); + char szBuf[8096]=""; + snprintf(szBuf,8096,"Old v2 model: texture coord frame count is not 1 [t = %d] meshIndex = %d modelFile [%s]",meshHeader.texCoordFrameCount,meshIndex,modelFile.c_str()); throw megaglest_runtime_error(szBuf); } @@ -318,8 +346,8 @@ void Mesh::loadV3(int meshIndex, const string &dir, FILE *f, if(meshHeader.normalFrameCount != meshHeader.vertexFrameCount) { - char szBuf[4096]=""; - sprintf(szBuf,"Old v3 model: vertex frame count different from normal frame count [v = %d, n = %d] meshIndex = %d modelFile [%s]",meshHeader.vertexFrameCount,meshHeader.normalFrameCount,meshIndex,modelFile.c_str()); + char szBuf[8096]=""; + snprintf(szBuf,8096,"Old v3 model: vertex frame count different from normal frame count [v = %d, n = %d] meshIndex = %d modelFile [%s]",meshHeader.vertexFrameCount,meshHeader.normalFrameCount,meshIndex,modelFile.c_str()); throw megaglest_runtime_error(szBuf); } @@ -674,7 +702,15 @@ void Mesh::save(int meshIndex, const string &dir, FILE *f, TextureManager *textu void Mesh::computeTangents(){ delete [] tangents; - tangents= new Vec3f[vertexCount]; + try { + tangents= new Vec3f[vertexCount]; + } + catch(bad_alloc& ba) { + char szBuf[8096]=""; + snprintf(szBuf,8096,"Error on line: %d size: %d msg: %s\n",__LINE__,vertexCount,ba.what()); + throw megaglest_runtime_error(szBuf); + } + for(unsigned int i=0; i