Skip to content
New issue

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

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

Already on GitHub? # to your account

Empty line ends on segfault #32

Open
jdreo opened this issue Sep 1, 2022 · 1 comment
Open

Empty line ends on segfault #32

jdreo opened this issue Sep 1, 2022 · 1 comment

Comments

@jdreo
Copy link

jdreo commented Sep 1, 2022

If the CSV file has an empty line, the parsing ends on a segfault.
At least the trailing newline should either be parsed out silently, or give raise to an explicit error rather than a segmentation fault.

Minimal example (can be copy/pasted into test/main.cpp):

TEST_CASE("Parse a SCSV string with column headers and trailing newline, using iterator-based loop" *
        test_suite("Reader")) {

  Reader<delimiter<' '>, quote_character<'"'>, first_row_is_header<true>> csv;
  const std::string buffer = "a b\nd 2 3\ne 5 6.7\n";

  csv.parse(buffer);

  const std::vector<std::string> expected_row_names{"d", "e"};
  const std::vector<double> expected_cell_values{2, 3, 5, 6.7};

  size_t rows=0, cells=0;
  for (auto row : csv) {
    auto icell = std::begin(row);
    std::string rname;
    (*icell).read_value(rname); // FIXME an operator-> would be expected to exists.
    REQUIRE(rname == expected_row_names[rows]);
    rows++;

    ++icell; // FIXME a postfix operator++ would be expected.
    for (; icell != std::end(row); ++icell) {
      std::string str;
      (*icell).read_raw_value(str);
      const double value = std::atof(str.c_str());
      REQUIRE(value == expected_cell_values[cells]);
      cells++;
    }
  }
  size_t cols = cells / rows;
  REQUIRE(rows == 2);
  REQUIRE(cols == 2);
}

Note that the code above advertises a use-case that was not documented: parsing a table having row headers.
In that case, using iterator-based loops makes sense. But the CellIterator interface —while functional— lacks the expected interface: operator-> and a postfix operator++.

@jdreo
Copy link
Author

jdreo commented Sep 1, 2022

At the very least, the documentation should mention iterator-based loops and the fact that the first iterator should be tested for the end of row. In which case, adding an operator== to CellIterator would be expected.

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

No branches or pull requests

1 participant