Don't cache stdout handle in Windows console colour impl

The cached handle would become invalid if some other code, say
a user-provided test code, redirects stdout through `freopen`
or `_dup2`, which would then cause AppVerifier to complain.

Fixes #2345
This commit is contained in:
Martin Hořeňovský 2022-01-29 23:12:40 +01:00
parent 7a2f9f4633
commit efb54926ee
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A

View File

@ -59,10 +59,10 @@ namespace {
class Win32ColourImpl : public IColourImpl { class Win32ColourImpl : public IColourImpl {
public: public:
Win32ColourImpl() : stdoutHandle( GetStdHandle(STD_OUTPUT_HANDLE) ) Win32ColourImpl() {
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo( stdoutHandle, &csbiInfo ); GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ),
&csbiInfo );
originalForegroundAttributes = csbiInfo.wAttributes & ~( BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY ); originalForegroundAttributes = csbiInfo.wAttributes & ~( BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY );
originalBackgroundAttributes = csbiInfo.wAttributes & ~( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY ); originalBackgroundAttributes = csbiInfo.wAttributes & ~( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY );
} }
@ -93,9 +93,10 @@ namespace {
private: private:
void setTextAttribute( WORD _textAttribute ) { void setTextAttribute( WORD _textAttribute ) {
SetConsoleTextAttribute( stdoutHandle, _textAttribute | originalBackgroundAttributes ); SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ),
_textAttribute |
originalBackgroundAttributes );
} }
HANDLE stdoutHandle;
WORD originalForegroundAttributes; WORD originalForegroundAttributes;
WORD originalBackgroundAttributes; WORD originalBackgroundAttributes;
}; };