From 13406b83b14db7a674c4ddd79f8089cac031ce92 Mon Sep 17 00:00:00 2001 From: MacDue Date: Tue, 26 Jul 2022 00:38:49 +0100 Subject: [PATCH] AK: VERIFY() the index is in bounds in StringView::operator[] That this did not already happen took me by surprise, as for most other similar containers/types in AK (e.g. Span) the index will be checked. This check not happening could easily let off-by-one indexing errors slip through the cracks. --- AK/StringView.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/AK/StringView.h b/AK/StringView.h index 17f0623f8c..f75786bc70 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -62,7 +62,12 @@ public: [[nodiscard]] ReadonlyBytes bytes() const { return { m_characters, m_length }; } - constexpr char const& operator[](size_t index) const { return m_characters[index]; } + constexpr char const& operator[](size_t index) const + { + if (!is_constant_evaluated()) + VERIFY(index < m_length); + return m_characters[index]; + } using ConstIterator = SimpleIterator;