Skip to content

Commit 3167f77

Browse files
fix out-of-bounds read in isBlackboardPointer whitespace trim (#1161)
Co-authored-by: Davide Faconti <davide.faconti@gmail.com>
1 parent edf12f1 commit 3167f77

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

‎src/tree_node.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,11 @@ bool TreeNode::isBlackboardPointer(StringView str, StringView* stripped_pointer)
396396
// strip leading and following spaces
397397
size_t front_index = 0;
398398
size_t last_index = str.size() - 1;
399-
while(str[front_index] == ' ' && front_index <= last_index)
399+
while(front_index <= last_index && str[front_index] == ' ')
400400
{
401401
front_index++;
402402
}
403-
while(str[last_index] == ' ' && front_index <= last_index)
403+
while(front_index <= last_index && str[last_index] == ' ')
404404
{
405405
last_index--;
406406
}

‎tests/gtest_ports.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,3 +869,21 @@ TEST(PortTest, SubtreeStringLiteralToLoopDouble_Issue1065)
869869
EXPECT_DOUBLE_EQ(collected[1], 2.0);
870870
EXPECT_DOUBLE_EQ(collected[2], 3.0);
871871
}
872+
873+
TEST(PortTest, IsBlackboardPointerAllWhitespaceView)
874+
{
875+
// A view made only of spaces must not be read past its end while trimming.
876+
// Use a buffer that is not null-terminated so AddressSanitizer catches the
877+
// over-read on the character after the last space.
878+
std::vector<char> only_spaces(3, ' ');
879+
const StringView spaces_view(only_spaces.data(), only_spaces.size());
880+
EXPECT_FALSE(TreeNode::isBlackboardPointer(spaces_view));
881+
882+
// Trimming and detection of real pointers is unchanged.
883+
StringView stripped;
884+
EXPECT_TRUE(TreeNode::isBlackboardPointer("{key}", &stripped));
885+
EXPECT_EQ(stripped, "key");
886+
EXPECT_TRUE(TreeNode::isBlackboardPointer(" {key} ", &stripped));
887+
EXPECT_EQ(stripped, "key");
888+
EXPECT_FALSE(TreeNode::isBlackboardPointer("value"));
889+
}

0 commit comments

Comments
 (0)