From 5d455454deda5c8e2e167e26cde51e093e7fd6f2 Mon Sep 17 00:00:00 2001 From: Dirk de la Hunt Date: Thu, 22 Oct 2015 20:04:14 +0200 Subject: [PATCH] Fix potential memory leak in console.c CAUTION: untested as my openssl installation has gone insane. If the incoming "hScreen" parameter is NULL, a new PSCREEN_RECORD is allocated to use in its place. However, if the allocation of the "pScreenBuf" member variable fails, the function returns, potentially leaking the newly allocated PSCREEN_RECORD. This fix first checks to see if the functions owns the "pScreenRec", and if so, frees it before returning. --- contrib/win32/win32compat/console.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contrib/win32/win32compat/console.c b/contrib/win32/win32compat/console.c index 4ed1b8b..df400cf 100644 --- a/contrib/win32/win32compat/console.c +++ b/contrib/win32/win32compat/console.c @@ -1608,7 +1608,13 @@ SCREEN_HANDLE ConSaveScreenHandle( SCREEN_HANDLE hScreen ) } if ( !pScreenRec->pScreenBuf ) + { + // if we allocated a screen within this scope, free it before returning + if ( pScreenRec != (PSCREEN_RECORD)hScreen ) { + free(pScreenRec); + } return NULL; + } result = ReadConsoleOutput( hConsole, // handle of a console screen buffer (PCHAR_INFO)(pScreenRec->pScreenBuf), // address of buffer that receives data