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

[bug] Crash in String Matcher #2931

Open
opmtk opened this issue Nov 13, 2024 · 3 comments
Open

[bug] Crash in String Matcher #2931

opmtk opened this issue Nov 13, 2024 · 3 comments

Comments

@opmtk
Copy link

opmtk commented Nov 13, 2024

Describe the bug

The following

        try {
            std::string a{ "string" };
            std::string const expected{ "string" };
            REQUIRE_THAT(a, Matches(expected));
        }
        catch (std::exception const& e) {
            std::cerr << std::format("caught '{}'\n", e.what());
        }

works as expected. If I get a bigger expected string (e.g. ~110kByte) it crashes with

...
[ctest] due to a fatal error condition:
[ctest]   SIGSEGV - Segmentation violation signal

The problem is related to the Matches matcher - the following doesn't crash:

        // Catch2's matchers haven't support for string_view
        std::string const expected_str{ csv_data::CsvSV };

        try {
            std::string a{ "string" };
            REQUIRE_THAT(expected_str, Matches(a));
        }
        catch (std::exception const& e) {
            std::cerr << std::format("caught '{}'\n", e.what());
        }

Expected behavior
I must not crash in that way, even I've a try/catch block/scope!

Reproduction steps

See above.

Platform information:

  • OS: Windows NT with devcontainer in VS Code running Ubuntu24.04
  • Compiler+version: gcc version 13.2.0 (Ubuntu 13.2.0-23ubuntu4)
  • Catch version: 3.4.0-1build1 amd64

Additional context

  • How can I ignore line endings with matchers? My reference data contains 16 rows with 161 columns in lines.
constexpr std::string_view const CsvSV =
R"(0,0.999994607194946,...
...
...,0.783392372240895
)";
  • How can a make a diff of the strings compared? E.g. only give the line number of difference?
@opmtk opmtk changed the title Crash in String Matcher [bug] Crash in String Matcher Nov 13, 2024
@horenmar
Copy link
Member

I can't reproduce this locally, nor on CE.

Are you sure that the segfault comes from the StringMatcher?

@opmtk
Copy link
Author

opmtk commented Nov 27, 2024

I'm still working on it (with breaks), but I did some tests and I'm not still sure about it. Note, I use C++20.

I'm redirecting ostream:

class StreamBufSentry {
    std::ostream& ostream;
    std::streambuf* rdbufBak;

public:
    ~StreamBufSentry() { ostream.rdbuf(rdbufBak); }
    StreamBufSentry(StreamBufSentry const&) = delete;
    StreamBufSentry(StreamBufSentry&&) = delete;

    StreamBufSentry(std::ostream& ostream)
        : ostream{ ostream }
        , rdbufBak{ ostream.rdbuf() }
    {
    }
};

and using it:

...
extern std::string_view const ActuatorLengthCsvSV; // test_csv_data.cpp
...
        ...
        StreamBufSentry sentry{ std::cout };
        std::ostringstream os;
        std::cout.rdbuf(os.rdbuf());

        CsvReader<double> csvReader{};
        ...
        csvReader.readFile(...);
         std::cout << csvReader << '\n';
         ...
        std::string const expected_str{ ActuatorLengthCsvSV };
        REQUIRE_THAT(expected_str, Matches(os.str()));         // boom
        ...        
)

//  test_csv_data.cpp
constexpr std::string_view const ActuatorLengthCsvSV = R"(0,0.999994607194946,....)";

@opmtk
Copy link
Author

opmtk commented Dec 11, 2024

I'll try to reduce it to a compile-able file

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

No branches or pull requests

2 participants