Skip to content

Commit

Permalink
defensive code against string out of bounds (apparently different beh…
Browse files Browse the repository at this point in the history
…aviour of libstdc++ vs. clang's libc++, can't read final NULL char w/ former)
  • Loading branch information
ochafik committed Jun 25, 2024
1 parent 48f417d commit 36bf003
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions common/json-schema-to-grammar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class string_view {
}

char operator[](size_t pos) const {
auto index = _start + pos;
if (index >= _end) {
throw std::out_of_range("string_view index out of range");
}
return _str[_start + pos];
}

Expand Down Expand Up @@ -110,13 +114,13 @@ static void _build_min_max_int(int min_value, int max_value, std::stringstream &
std::function<void(const string_view &, const string_view &)> uniform_range =
[&](const string_view & from, const string_view & to) {
size_t i = 0;
while (from[i] == to[i]) {
while (i < from.length() && i < to.length() && from[i] == to[i]) {
i++;
}
if (i > 0) {
out << "\"" << from.substr(0, i).str() << "\"";
}
if (i < from.length()) {
if (i < from.length() && i < to.length()) {
if (i > 0) {
out << " ";
}
Expand Down

0 comments on commit 36bf003

Please sign in to comment.