From 1ca9753e10a67384a66f604eb9197ea3ef6c60b7 Mon Sep 17 00:00:00 2001 From: Kyzentun Date: Sun, 7 Sep 2014 14:09:39 -0600 Subject: [PATCH] Sanity checking for frame and texuture hints from filenames. --- src/RageBitmapTexture.cpp | 11 +++++++++-- src/RageTexture.cpp | 27 +++++++++++++++++++++++---- src/RageTexture.h | 2 +- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/RageBitmapTexture.cpp b/src/RageBitmapTexture.cpp index 3c58fc0b82..17252e7f59 100644 --- a/src/RageBitmapTexture.cpp +++ b/src/RageBitmapTexture.cpp @@ -26,8 +26,15 @@ static void GetResolutionFromFileName( RString sPath, int &iWidth, int &iHeight if( !re.Compare(sPath, asMatches) ) return; - iWidth = StringToInt( asMatches[0] ); - iHeight = StringToInt( asMatches[1] ); + // Check for nonsense values. Some people might not intend the hint. -Kyz + int maybe_width= StringToInt(asMatches[0]); + int maybe_height= StringToInt(asMatches[1]); + if(maybe_width <= 0 || maybe_height <= 0 || maybe_width > iWidth || maybe_height > iHeight) + { + return; + } + iWidth = maybe_width; + iHeight = maybe_height; } RageBitmapTexture::RageBitmapTexture( RageTextureID name ) : diff --git a/src/RageTexture.cpp b/src/RageTexture.cpp index 88d17d97d2..f0d35a3203 100644 --- a/src/RageTexture.cpp +++ b/src/RageTexture.cpp @@ -22,7 +22,7 @@ RageTexture::~RageTexture() void RageTexture::CreateFrameRects() { - GetFrameDimensionsFromFileName( GetID().filename, &m_iFramesWide, &m_iFramesHigh ); + GetFrameDimensionsFromFileName( GetID().filename, &m_iFramesWide, &m_iFramesHigh, m_iSourceWidth, m_iSourceHeight ); // Fill in the m_FrameRects with the bounds of each frame in the animation. m_TextureCoordRects.clear(); @@ -42,7 +42,7 @@ void RageTexture::CreateFrameRects() } } -void RageTexture::GetFrameDimensionsFromFileName( RString sPath, int* piFramesWide, int* piFramesHigh ) +void RageTexture::GetFrameDimensionsFromFileName( RString sPath, int* piFramesWide, int* piFramesHigh, int source_width, int source_height ) { static Regex match( " ([0-9]+)x([0-9]+)([\\. ]|$)" ); vector asMatch; @@ -51,8 +51,27 @@ void RageTexture::GetFrameDimensionsFromFileName( RString sPath, int* piFramesWi *piFramesWide = *piFramesHigh = 1; return; } - *piFramesWide = StringToInt(asMatch[0]); - *piFramesHigh = StringToInt(asMatch[1]); + // Check for nonsense values. Some people might not intend the hint. -Kyz + int maybe_width= StringToInt(asMatch[0]); + int maybe_height= StringToInt(asMatch[1]); + if(maybe_width <= 0 || maybe_height <= 0) + { + *piFramesWide = *piFramesHigh = 1; + return; + } + // Font.cpp uses this function, but can't pass in a texture size. Other + // textures can pass in a size though, and having more frames than pixels + // makes no sense. -Kyz + if(source_width > 0 && source_height > 0) + { + if(maybe_width > source_width || maybe_height > source_height) + { + *piFramesWide = *piFramesHigh = 1; + return; + } + } + *piFramesWide = maybe_width; + *piFramesHigh = maybe_height; } const RectF *RageTexture::GetTextureCoordRect( int iFrameNo ) const diff --git a/src/RageTexture.h b/src/RageTexture.h index 10840c4a82..043cf03a7f 100644 --- a/src/RageTexture.h +++ b/src/RageTexture.h @@ -61,7 +61,7 @@ public: // The ID that we were asked to load: const RageTextureID &GetID() const { return m_ID; } - static void GetFrameDimensionsFromFileName( RString sPath, int* puFramesWide, int* puFramesHigh ); + static void GetFrameDimensionsFromFileName( RString sPath, int* puFramesWide, int* puFramesHigh, int source_width= 0, int source_height= 0 ); // Lua virtual void PushSelf( lua_State *L );