ByteVector, List: Check indices if the standard library does (#1467)

If the standard library checks the index in std::vector::operator[]
(_GLIBCXX_ASSERTIONS, libc++ hardening modes), ByteVector::operator[]
and List::operator[] now check the index against their own size and
abort if it is out of range, as std::vector does.

The std::vector check alone is not enough: a ByteVector created by
mid() shares the buffer of the original byte vector, so an index past
the end of the smaller vector still passes it. List::operator[] walks
a std::list, which these checks do not cover.

Builds without these macros are unchanged; a GCC Release build of
libtag produces identical code. The Ubuntu CI job now builds with
-D_GLIBCXX_ASSERTIONS so that the tests run with the checks enabled.
This commit is contained in:
Nathan Chandrasekar
2026-10-31 12:07:55 +01:00
committed by GitHub
parent cfb9a7d8a9
commit 50ca520b27
4 changed files with 60 additions and 0 deletions
+16
View File
@@ -50,6 +50,7 @@ class TestByteVector : public CppUnit::TestFixture
CPPUNIT_TEST(testReplace);
CPPUNIT_TEST(testReplaceAndDetach);
CPPUNIT_TEST(testIterator);
CPPUNIT_TEST(testIndex);
CPPUNIT_TEST(testResize);
CPPUNIT_TEST(testAppend1);
CPPUNIT_TEST(testAppend2);
@@ -420,6 +421,21 @@ public:
CPPUNIT_ASSERT_EQUAL('3', *it4);
}
void testIndex()
{
const ByteVector a("0123456789");
const ByteVector b = a.mid(3, 4);
CPPUNIT_ASSERT_EQUAL('3', b[0]);
CPPUNIT_ASSERT_EQUAL('6', b[3]);
ByteVector c = a.mid(7);
CPPUNIT_ASSERT_EQUAL('7', c[0]);
CPPUNIT_ASSERT_EQUAL('9', c[2]);
c[2] = 'X';
CPPUNIT_ASSERT_EQUAL(ByteVector("78X"), c);
CPPUNIT_ASSERT_EQUAL('9', a[9]);
}
void testResize()
{
auto a = ByteVector("0123456789");