From 0980e90ee486de80b082bf77c3e4762b885b4d67 Mon Sep 17 00:00:00 2001 From: Mario Liebisch Date: Thu, 22 Jun 2017 11:24:53 +0200 Subject: [PATCH] Fixed potential buffer overrun in Win32 OpenGL error handling This fixes issue #1245. --- src/SFML/Window/Win32/WglContext.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/SFML/Window/Win32/WglContext.cpp b/src/SFML/Window/Win32/WglContext.cpp index 09496082..57516032 100644 --- a/src/SFML/Window/Win32/WglContext.cpp +++ b/src/SFML/Window/Win32/WglContext.cpp @@ -66,13 +66,17 @@ void ensureExtensionsInit(HDC deviceContext) //////////////////////////////////////////////////////////// String getErrorString(DWORD errorCode) { - std::basic_ostringstream > ss; - TCHAR errBuff[256]; - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errorCode, 0, errBuff, sizeof(errBuff), NULL); - ss << errBuff; - String errMsg(ss.str()); + PTCHAR buffer; + if (FormatMessage(FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorCode, 0, reinterpret_cast(&buffer), 256, NULL) != 0) + { + String errMsg(buffer); + LocalFree(buffer); + return errMsg; + } - return errMsg; + std::ostringstream ss; + ss << "Error " << errorCode; + return String(ss.str()); }