hanickadot / compile-time-regular-expressions

Compile Time Regular Expression in C++
https://twitter.com/hankadusikova
Apache License 2.0
3.22k stars 177 forks source link

to_view() tries to dereference out of range iterator #296

Closed Sleafar closed 8 months ago

Sleafar commented 8 months ago

Code to reproduce:

#include <iostream>
#include <ctre.hpp>

int main() {
    std::string data = "a\n\nb\n";
    auto lines = ctre::split<"\n">(data);
    for (auto line: lines) {
        //auto t = line.to_string(); //this works
        auto t = line.to_view(); //this doesn't
        std::cout << ">" << t << "<" << std::endl;
    }
}

If this is run in debug mode with MSVC 19.37.32824, it triggers a check in _String_const_iterator.operator*() which detects out of range access. This problem seems only to occur for empty matches at the end of the data.

Sleafar commented 8 months ago

The problem is in line 80 in return_type.hpp:

return &*_begin;

Alcaro commented 8 months ago

Creating a reference to past-the-end counts as accessing it, even if the only thing done is turning it back into a pointer.

It's completely harmless in practice, but yes, compilers tend to be whiny. C++11 added .data() to std::vector for exactly that reason. (Though I suspect that function isn't the solution here.)

hanickadot commented 8 months ago

@Sleafar can you try it now? (I don't have MSVC available)

Sleafar commented 8 months ago

@hanickadot I don't get the error anymore with the current main branch.

hanickadot commented 8 months ago

Thanks for reporting and the check!