From 2f0c06234cae453b39e67c8eaa5d51191ded7c7b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jarkko=20P=C3=B6yry?= Date: Tue, 10 Feb 2015 13:54:51 -0800 Subject: [PATCH] Fix off-by-one in info log query. - Fix off-by-one in info log query causing infolog string to contain 0-byte. - Tolerate and work around GL implementation off-by-ones in infolog query. Change-Id: I94f2ac55ed9f7a341055f28c69d36a4e0f213481 --- framework/opengl/gluShaderProgram.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/framework/opengl/gluShaderProgram.cpp b/framework/opengl/gluShaderProgram.cpp index 1b47046f7..3070bc816 100644 --- a/framework/opengl/gluShaderProgram.cpp +++ b/framework/opengl/gluShaderProgram.cpp @@ -123,17 +123,29 @@ static bool getProgramLinkStatus (const glw::Functions& gl, deUint32 program) static std::string getProgramInfoLog (const glw::Functions& gl, deUint32 program) { - int infoLogLen = 0; - int unusedLen; + int infoLogLen = 0; + int unusedLen; - gl.getProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLen); + gl.getProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLen); GLU_EXPECT_NO_ERROR(gl.getError(), "glGetProgramiv()"); if (infoLogLen > 0) { - std::vector infoLog(infoLogLen); - gl.getProgramInfoLog(program, (int)infoLog.size(), &unusedLen, &infoLog[0]); - return std::string(&infoLog[0], infoLogLen); + // The INFO_LOG_LENGTH query and the buffer query implementations have + // very commonly off-by-one errors. Try to work around these issues. + + // add tolerance for off-by-one in log length, buffer write, and for terminator + std::vector infoLog(infoLogLen + 3, '\0'); + + // claim buf size is one smaller to protect from off-by-one writing over buffer bounds + gl.getProgramInfoLog(program, (int)infoLog.size() - 1, &unusedLen, &infoLog[0]); + + // return whole buffer if null terminator was overwritten + if (infoLog[(int)(infoLog.size()) - 1] != '\0') + return std::string(&infoLog[0], infoLog.size()); + + // read as C string. infoLog is guaranteed to be 0-terminated + return std::string(&infoLog[0]); } return std::string(); } -- 2.34.1