From e1b25f323e128bf990b6b8e78633b2e8e20aeb8a Mon Sep 17 00:00:00 2001 From: "Matias N. Goldberg" Date: Wed, 1 May 2024 15:33:26 -0300 Subject: [PATCH] [GL3+] Fix leak parsing GLSL shaders (#445) --- .../GL3Plus/include/GLSL/OgreGLSLPreprocessor.h | 10 ++++++++++ .../GL3Plus/src/GLSL/OgreGLSLPreprocessor.cpp | 8 +++++++- RenderSystems/GL3Plus/src/GLSL/OgreGLSLShader.cpp | 3 +-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/RenderSystems/GL3Plus/include/GLSL/OgreGLSLPreprocessor.h b/RenderSystems/GL3Plus/include/GLSL/OgreGLSLPreprocessor.h index 0814cefa571..0237c6b879e 100644 --- a/RenderSystems/GL3Plus/include/GLSL/OgreGLSLPreprocessor.h +++ b/RenderSystems/GL3Plus/include/GLSL/OgreGLSLPreprocessor.h @@ -508,6 +508,16 @@ namespace Ogre { */ char *Parse (const char *iSource, size_t iLength, size_t &oLength); + /** Parse the input string as a preamble (i.e. to define a lot of enums). + @param iSource + The source text containing preamble macros. + @param iLength + The length of iSource text in characters. + @return + False on errors. True on success. + */ + bool ParsePreamble( const char *iSource, size_t iLength ); + /** * An error handler function type. * The default implementation just drops a note to stderr and diff --git a/RenderSystems/GL3Plus/src/GLSL/OgreGLSLPreprocessor.cpp b/RenderSystems/GL3Plus/src/GLSL/OgreGLSLPreprocessor.cpp index 4189b949faa..0377096eb59 100644 --- a/RenderSystems/GL3Plus/src/GLSL/OgreGLSLPreprocessor.cpp +++ b/RenderSystems/GL3Plus/src/GLSL/OgreGLSLPreprocessor.cpp @@ -1442,4 +1442,10 @@ namespace Ogre { return retval.Buffer; } -} // namespace Ogre + bool CPreprocessor::ParsePreamble( const char *iSource, size_t iLength ) + { + Token retval = Parse( Token( Token::TK_TEXT, iSource, iLength ) ); + return retval.Type != Token::TK_ERROR; + } + +} // namespace Ogre diff --git a/RenderSystems/GL3Plus/src/GLSL/OgreGLSLShader.cpp b/RenderSystems/GL3Plus/src/GLSL/OgreGLSLShader.cpp index f87ac651022..a484bc474e6 100644 --- a/RenderSystems/GL3Plus/src/GLSL/OgreGLSLShader.cpp +++ b/RenderSystems/GL3Plus/src/GLSL/OgreGLSLShader.cpp @@ -167,7 +167,6 @@ namespace Ogre { replaceVersionMacros(); // Mask out vulkan_layout() macros - size_t unusedVal = 0; const String preamble = "#define vulkan_layout(x)\n" "#define vulkan( x )\n" @@ -184,7 +183,7 @@ namespace Ogre { "#define vkSampler2DArray( a, b ) a\n" "#define vkSampler3D( a, b ) a\n" "#define vkSamplerCube( a, b ) a\n"; - cpp.Parse( preamble.c_str(), preamble.size(), unusedVal ); + cpp.ParsePreamble( preamble.c_str(), preamble.size() ); // Pass all user-defined macros to preprocessor if (!mPreprocessorDefines.empty ())