From 9d1e5d9acf1ece3652d4e66973b6e81341cfe2e7 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Wed, 25 Sep 2024 10:14:12 -0600 Subject: [PATCH] Prevent reading from an uninitialized character buffer If `GLEXT_glGetInfoLog` fails then `log` is left unchanged which can lead to a reading from an uninitialized buffer on the following line. Initializing the buffer ensures this never happens. While I was touching this code I elected to switch to `std::array` as well since SFML has made previous efforts to stop using C-style arrays and I wanted to continue that effort. --- src/SFML/Graphics/Shader.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/SFML/Graphics/Shader.cpp b/src/SFML/Graphics/Shader.cpp index 9367f190d..03b89b169 100644 --- a/src/SFML/Graphics/Shader.cpp +++ b/src/SFML/Graphics/Shader.cpp @@ -40,6 +40,7 @@ #include #include +#include #include #include #include @@ -882,9 +883,9 @@ bool Shader::compile(std::string_view vertexShaderCode, std::string_view geometr glCheck(GLEXT_glGetObjectParameteriv(vertexShader, GLEXT_GL_OBJECT_COMPILE_STATUS, &success)); if (success == GL_FALSE) { - char log[1024]; - glCheck(GLEXT_glGetInfoLog(vertexShader, sizeof(log), nullptr, log)); - err() << "Failed to compile vertex shader:" << '\n' << log << std::endl; + std::array log{}; + glCheck(GLEXT_glGetInfoLog(vertexShader, sizeof(log), nullptr, log.data())); + err() << "Failed to compile vertex shader:" << '\n' << log.data() << std::endl; glCheck(GLEXT_glDeleteObject(vertexShader)); glCheck(GLEXT_glDeleteObject(shaderProgram)); return false; @@ -910,9 +911,9 @@ bool Shader::compile(std::string_view vertexShaderCode, std::string_view geometr glCheck(GLEXT_glGetObjectParameteriv(geometryShader, GLEXT_GL_OBJECT_COMPILE_STATUS, &success)); if (success == GL_FALSE) { - char log[1024]; - glCheck(GLEXT_glGetInfoLog(geometryShader, sizeof(log), nullptr, log)); - err() << "Failed to compile geometry shader:" << '\n' << log << std::endl; + std::array log{}; + glCheck(GLEXT_glGetInfoLog(geometryShader, sizeof(log), nullptr, log.data())); + err() << "Failed to compile geometry shader:" << '\n' << log.data() << std::endl; glCheck(GLEXT_glDeleteObject(geometryShader)); glCheck(GLEXT_glDeleteObject(shaderProgram)); return false; @@ -938,9 +939,9 @@ bool Shader::compile(std::string_view vertexShaderCode, std::string_view geometr glCheck(GLEXT_glGetObjectParameteriv(fragmentShader, GLEXT_GL_OBJECT_COMPILE_STATUS, &success)); if (success == GL_FALSE) { - char log[1024]; - glCheck(GLEXT_glGetInfoLog(fragmentShader, sizeof(log), nullptr, log)); - err() << "Failed to compile fragment shader:" << '\n' << log << std::endl; + std::array log{}; + glCheck(GLEXT_glGetInfoLog(fragmentShader, sizeof(log), nullptr, log.data())); + err() << "Failed to compile fragment shader:" << '\n' << log.data() << std::endl; glCheck(GLEXT_glDeleteObject(fragmentShader)); glCheck(GLEXT_glDeleteObject(shaderProgram)); return false; @@ -959,9 +960,9 @@ bool Shader::compile(std::string_view vertexShaderCode, std::string_view geometr glCheck(GLEXT_glGetObjectParameteriv(shaderProgram, GLEXT_GL_OBJECT_LINK_STATUS, &success)); if (success == GL_FALSE) { - char log[1024]; - glCheck(GLEXT_glGetInfoLog(shaderProgram, sizeof(log), nullptr, log)); - err() << "Failed to link shader:" << '\n' << log << std::endl; + std::array log{}; + glCheck(GLEXT_glGetInfoLog(shaderProgram, sizeof(log), nullptr, log.data())); + err() << "Failed to link shader:" << '\n' << log.data() << std::endl; glCheck(GLEXT_glDeleteObject(shaderProgram)); return false; }