From a76f7e9030557e9b074f7303a00e3e5fbabd4f60 Mon Sep 17 00:00:00 2001 From: Henner Zeller Date: Mon, 24 Feb 2020 14:00:31 -0800 Subject: [PATCH] Prevent compiler-warning of possible out-of-bounds access. PiperOrigin-RevId: 296967895 --- common/text/tree_utils_test.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/common/text/tree_utils_test.cc b/common/text/tree_utils_test.cc index a6182d8d9..4d8520213 100644 --- a/common/text/tree_utils_test.cc +++ b/common/text/tree_utils_test.cc @@ -618,7 +618,10 @@ TEST_F(FindSubtreeStartingAtOffsetTest, LeafOnlyOffsetGreaterThan) { EXPECT_EQ(subtree, nullptr); } -constexpr absl::string_view kBaseText("abcdefghijklmnopqrst"); +// Allow to look beyond kBaseText by cutting it out of a larger string. +constexpr absl::string_view kBaseTextPadded("_abcdefghijklmnopqrst_"); +constexpr absl::string_view kBaseText = {kBaseTextPadded.data() + 1, + kBaseTextPadded.length() - 2}; // Return a tree with monotonically increasing token locations. // This is suitable for tests that require only location offsets, @@ -644,6 +647,7 @@ struct FindSubtreeStartingAtOffsetFakeTreeTest : public testing::Test { // Test that a whole tree is returned when offset is less than leftmost token. TEST_F(FindSubtreeStartingAtOffsetFakeTreeTest, TreeOffsetLessThanFirstToken) { + // Note: begin() - 1 points to a valid location due to kBaseTextPadded SymbolPtr* subtree = FindSubtreeStartingAtOffset(&tree, kBaseText.begin() - 1); EXPECT_EQ(*subtree, tree);