From abd31ca4e2bd808f6b0e949f1217d6e983039cdd Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Thu, 4 Jul 2013 06:29:02 -0300 Subject: [PATCH] Fixed ParticleSystems and Files crashing instead of erroring when given bad sizes (issue #666) --- src/modules/filesystem/physfs/File.cpp | 3 +++ src/modules/graphics/opengl/ParticleSystem.cpp | 5 ++++- src/modules/graphics/opengl/ParticleSystem.h | 4 ++-- src/modules/graphics/opengl/wrap_Graphics.cpp | 10 +++++++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/modules/filesystem/physfs/File.cpp b/src/modules/filesystem/physfs/File.cpp index 32713886a..ad691ced7 100644 --- a/src/modules/filesystem/physfs/File.cpp +++ b/src/modules/filesystem/physfs/File.cpp @@ -136,6 +136,9 @@ FileData *File::read(int64 size) int64 cur = tell(); size = (size == ALL) ? max : size; + if (size < 0) + throw love::Exception("Invalid read size."); + // Clamping because the file offset may be in a weird position. if (cur < 0) cur = 0; diff --git a/src/modules/graphics/opengl/ParticleSystem.cpp b/src/modules/graphics/opengl/ParticleSystem.cpp index 0a8471569..f7d52bf81 100644 --- a/src/modules/graphics/opengl/ParticleSystem.cpp +++ b/src/modules/graphics/opengl/ParticleSystem.cpp @@ -63,7 +63,7 @@ StringMap ParticleSystem::distributions(ParticleSystem::distributionsEntries, sizeof(ParticleSystem::distributionsEntries)); -ParticleSystem::ParticleSystem(Image *image, unsigned int buffer) +ParticleSystem::ParticleSystem(Image *image, int buffer) : pStart(0) , pLast(0) , pEnd(0) @@ -95,6 +95,9 @@ ParticleSystem::ParticleSystem(Image *image, unsigned int buffer) , offsetX(image->getWidth()*0.5f) , offsetY(image->getHeight()*0.5f) { + if (buffer <= 0) + throw love::Exception("Invalid ParticleSystem size."); + sizes.push_back(1.0f); colors.push_back(Colorf(1.0f, 1.0f, 1.0f, 1.0f)); setBufferSize(buffer); diff --git a/src/modules/graphics/opengl/ParticleSystem.h b/src/modules/graphics/opengl/ParticleSystem.h index 24a463fa8..1de161629 100644 --- a/src/modules/graphics/opengl/ParticleSystem.h +++ b/src/modules/graphics/opengl/ParticleSystem.h @@ -86,7 +86,7 @@ public: /** * Creates a particle system with the specified buffersize and image. **/ - ParticleSystem(Image *image, unsigned int buffer); + ParticleSystem(Image *image, int buffer); /** * Deletes any allocated memory. @@ -503,7 +503,7 @@ public: protected: // The max amount of particles. - unsigned int bufferSize; + int bufferSize; // Pointer to the first particle. particle *pStart; diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index 5f54a900f..8d38ce7de 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -409,7 +409,15 @@ int w_newParticleSystem(lua_State *L) { Image *image = luax_checkimage(L, 1); int size = luaL_optint(L, 2, 1000); - ParticleSystem *t = instance->newParticleSystem(image, size); + ParticleSystem *t = 0; + try + { + t = instance->newParticleSystem(image, size); + } + catch (love::Exception &e) + { + return luaL_error(L, "%s", e.what()); + } luax_newtype(L, "ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_T, (void *)t); return 1; }