defensive code against string out of bounds (apparently different behaviour of libstdc++ vs. clang's libc++, can't read final NULL char w/ former)

This commit is contained in:
Olivier Chafik 2024-06-25 14:09:22 +01:00
parent 48f417db32
commit 36bf00369a

View file

@ -69,6 +69,10 @@ public:
} }
char operator[](size_t pos) const { 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]; return _str[_start + pos];
} }
@ -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 = std::function<void(const string_view &, const string_view &)> uniform_range =
[&](const string_view & from, const string_view & to) { [&](const string_view & from, const string_view & to) {
size_t i = 0; size_t i = 0;
while (from[i] == to[i]) { while (i < from.length() && i < to.length() && from[i] == to[i]) {
i++; i++;
} }
if (i > 0) { if (i > 0) {
out << "\"" << from.substr(0, i).str() << "\""; out << "\"" << from.substr(0, i).str() << "\"";
} }
if (i < from.length()) { if (i < from.length() && i < to.length()) {
if (i > 0) { if (i > 0) {
out << " "; out << " ";
} }