vincentlaucsb / csv-parser

A high-performance, fully-featured CSV parser and serializer for modern C++.
MIT License
901 stars 150 forks source link

Issues when using non null-terminated string views. #232

Open UltimateEvil opened 4 months ago

UltimateEvil commented 4 months ago

I, for whichever reson, have a string which contains a CSV as a substring.

Currently when calling parse("1,2,3,4"sv.substr(2),format) the following code is executed

CSVReader parse(csv::string_view in, CSVFormat format) {
        std::stringstream stream(in.data());
        return CSVReader(stream, format);
    }

This will read memory outside the string view due to the following constructor bing invoked:

explicit basic_stringstream
    ( std::basic_string<CharT, Traits, Allocator>&& str,
      std::ios_base::openmode mode =
          std::ios_base::in | std::ios_base::out);

In c++ 26 the following is fine

CSVReader parse(csv::string_view in, CSVFormat format) {
        std::stringstream stream(in);
        return CSVReader(stream, format);
    }

Untill then, please document this behaviour or change it so the call is instead using a correctly constructed string

CSVReader parse(csv::string_view in, CSVFormat format) {
        std::stringstream stream(std::string{in});
        return CSVReader(stream, format);
    }