From 36bf00369a2736473a878b12d773d363b97103d3 Mon Sep 17 00:00:00 2001 From: Olivier Chafik Date: Tue, 25 Jun 2024 14:09:22 +0100 Subject: [PATCH] defensive code against string out of bounds (apparently different behaviour of libstdc++ vs. clang's libc++, can't read final NULL char w/ former) --- common/json-schema-to-grammar.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp index 2c47645cf..07d0e952d 100644 --- a/common/json-schema-to-grammar.cpp +++ b/common/json-schema-to-grammar.cpp @@ -69,6 +69,10 @@ public: } 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]; } @@ -110,13 +114,13 @@ static void _build_min_max_int(int min_value, int max_value, std::stringstream & std::function 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 << " "; }