From efb8efb45e5abb789ebd7d5362474ab528a3e1a0 Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Wed, 8 Jun 2011 22:45:28 +0200 Subject: [PATCH] Prevent overflows and underflows in love.graphics.push and love.graphics.pop (bug #118) Maximum is hardware maximum - 5, so the engine itself has a few matrices. --- src/modules/graphics/opengl/Graphics.cpp | 13 ++++++++++- src/modules/graphics/opengl/Graphics.h | 2 ++ src/modules/graphics/opengl/wrap_Graphics.cpp | 22 +++++++++++++++---- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index fa94a7853..a404423a9 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -37,7 +37,7 @@ namespace opengl { Graphics::Graphics() - : currentFont(0), lineWidth(1) + : currentFont(0), lineWidth(1), matrixLimit(0), userMatrices(0) { // Indicates that there is no screen // created yet. @@ -279,6 +279,11 @@ namespace opengl // Restore the display state. restoreState(tempState); + + // Get the maximum number of matrices + // subtract a few to give the engine some room. + glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH, &matrixLimit); + matrixLimit -= 5; return true; } @@ -984,12 +989,18 @@ namespace opengl void Graphics::push() { + if (userMatrices == matrixLimit) + throw Exception("Maximum stack depth reached."); glPushMatrix(); + ++userMatrices; } void Graphics::pop() { + if (userMatrices < 1) + throw Exception("Minimum stack depth reached. (More pops than pushes?)"); glPopMatrix(); + --userMatrices; } void Graphics::rotate(float r) diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index 6e0e1ae92..7f1b88e09 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -115,6 +115,8 @@ namespace opengl DisplayMode currentMode; float lineWidth; + GLint matrixLimit; + GLint userMatrices; public: diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index 8fdb63f64..078f6c2a5 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -950,15 +950,29 @@ namespace opengl return 0; } - int w_push(lua_State *) + int w_push(lua_State *L) { - instance->push(); + try + { + instance->push(); + } + catch (love::Exception e) + { + return luaL_error(L, e.what()); + } return 0; } - int w_pop(lua_State *) + int w_pop(lua_State *L) { - instance->pop(); + try + { + instance->pop(); + } + catch (love::Exception e) + { + return luaL_error(L, e.what()); + } return 0; }