Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Win32ColourImpl has problems with tests that redirect stdout #2345

Closed
dthaler opened this issue Jan 4, 2022 · 3 comments
Closed

Win32ColourImpl has problems with tests that redirect stdout #2345

dthaler opened this issue Jan 4, 2022 · 3 comments
Labels

Comments

@dthaler
Copy link

dthaler commented Jan 4, 2022

Describe the bug
Win32ColourImpl saves GetStdHandle(STD_OUTPUT_HANDLE) and then assumes the handle will be valid throughout the test. This assumption is not true if the test uses freopen or _dup2() to redirect stdout, which closes the underlying handle. This results in setTextAttribute() passing an invalid handle to SetConsoleTextAttribute(). If appverif is enabled for handle checking, this hits an app verifier exception.

Expected behavior
Color should work normally, even with application verifier enabled for handle checking, and even when the test being executed redirects stdout and then restores it.

Reproduction steps
Create a test file as follows:

#include <io.h>
#define CATCH_CONFIG_MAIN
#include "catch2\catch.hpp"

TEST_CASE("test", "[test]")
{
    HANDLE original_handle = GetStdHandle(STD_OUTPUT_HANDLE);
    int fdDup = _dup(_fileno(stdout));
    FILE* fp;
    freopen_s(&fp, "stdout.out", "w", stdout); // Redirect stdout to a file.
    printf("Hello world\n");
    _dup2(fdDup, _fileno(stdout)); // Restore stdout to original.
    _close(fdDup);
    HANDLE new_handle = GetStdHandle(STD_OUTPUT_HANDLE);
    assert(new_handle != original_handle);
}

Enable application verifier handle checking: appverif -enable handles -for test.exe
Run "test.exe [test]" under a debugger. E.g., in visual studio set the debugger arguments to "[test]". The use of a filter will force catch to initialize Win32ColourImpl before the test runs, and then try to use the handle after the test completes.

Platform information:
OS: Windows 10
Compiler version: Visual Studio 2019
Catch version: v2.8.0 using the CatchOrg.Catch nuget package

@horenmar
Copy link
Member

So, if I understand this correctly, the handle needs to be reloaded before every SetConsoleTextAttribute?

@horenmar horenmar added the Bug label Jan 29, 2022
@dthaler
Copy link
Author

dthaler commented Jan 29, 2022

So, if I understand this correctly, the handle needs to be reloaded before every SetConsoleTextAttribute?

Yes.

@horenmar
Copy link
Member

Ok.

I might end up tearing out the Windows cmd colouring support (supporting it together with multireporters is going to be pain and half), but that's an easy enough fix to merge it in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants