Skip to content

Commit

Permalink
pw_log_rpc: Fix out-of-range memory access
Browse files Browse the repository at this point in the history
The tests of pw_log_rpc attempt to access out-of-range array indicies.
This change resizes the arrays to properly hold the test contents.

Bug: b/239856986
Change-Id: I0be710253a1039e91d0258270fcfec6798def17e
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/103790
Pigweed-Auto-Submit: Armando Montanez <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
Reviewed-by: Carlos Chinchilla <[email protected]>
  • Loading branch information
armandomontanez authored and CQ Bot Account committed Jul 29, 2022
1 parent 07a61cc commit 71b85f8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 11 additions & 8 deletions pw_log_rpc/log_filter_service_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class FilterServiceTest : public ::testing::Test {

protected:
FilterMap filter_map_;
static constexpr size_t kMaxFilterRules = 3;
static constexpr size_t kMaxFilterRules = 4;
std::array<Filter::Rule, kMaxFilterRules> rules1_;
std::array<Filter::Rule, kMaxFilterRules> rules2_;
std::array<Filter::Rule, kMaxFilterRules> rules3_;
Expand Down Expand Up @@ -137,7 +137,7 @@ void VerifyRule(const Filter::Rule& rule, const Filter::Rule& expected_rule) {
}

TEST_F(FilterServiceTest, SetFilterRules) {
const std::array<Filter::Rule, 4> new_rules{{
const std::array<Filter::Rule, kMaxFilterRules> new_rules{{
{
.action = Filter::Rule::Action::kKeep,
.level_greater_than_or_equal = log::FilterRule::Level::DEBUG_LEVEL,
Expand Down Expand Up @@ -175,8 +175,9 @@ TEST_F(FilterServiceTest, SetFilterRules) {
std::byte('R')},
},
}};
const Filter new_filter(filters_[0].id(),
const_cast<std::array<Filter::Rule, 4>&>(new_rules));
const Filter new_filter(
filters_[0].id(),
const_cast<std::array<Filter::Rule, kMaxFilterRules>&>(new_rules));

std::byte request_buffer[512];
const auto request = EncodeFilterRequest(new_filter, request_buffer);
Expand All @@ -194,7 +195,7 @@ TEST_F(FilterServiceTest, SetFilterRules) {
}

TEST_F(FilterServiceTest, SetFilterRulesWhenUsedByDrain) {
const std::array<Filter::Rule, 4> new_filter_rules{{
const std::array<Filter::Rule, kMaxFilterRules> new_filter_rules{{
{
.action = Filter::Rule::Action::kKeep,
.level_greater_than_or_equal = log::FilterRule::Level::CRITICAL_LEVEL,
Expand Down Expand Up @@ -238,7 +239,8 @@ TEST_F(FilterServiceTest, SetFilterRulesWhenUsedByDrain) {
}};
Filter& filter = filters_[0];
const Filter new_filter(
filter.id(), const_cast<std::array<Filter::Rule, 4>&>(new_filter_rules));
filter.id(),
const_cast<std::array<Filter::Rule, kMaxFilterRules>&>(new_filter_rules));

std::byte request_buffer[256];
const auto request = EncodeFilterRequest(new_filter, request_buffer);
Expand Down Expand Up @@ -266,7 +268,7 @@ TEST_F(FilterServiceTest, SetFilterRulesWhenUsedByDrain) {
}

// A new request for logs with a new filter updates filter.
const std::array<Filter::Rule, 4> second_filter_rules{{
const std::array<Filter::Rule, kMaxFilterRules> second_filter_rules{{
{
.action = Filter::Rule::Action::kKeep,
.level_greater_than_or_equal = log::FilterRule::Level::DEBUG_LEVEL,
Expand Down Expand Up @@ -298,7 +300,8 @@ TEST_F(FilterServiceTest, SetFilterRulesWhenUsedByDrain) {
}};
const Filter second_filter(
filter.id(),
const_cast<std::array<Filter::Rule, 4>&>(second_filter_rules));
const_cast<std::array<Filter::Rule, kMaxFilterRules>&>(
second_filter_rules));

std::memset(request_buffer, 0, sizeof(request_buffer));
const auto second_filter_request =
Expand Down
2 changes: 1 addition & 1 deletion pw_log_rpc/log_service_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class LogServiceTest : public ::testing::Test {
multisink::MultiSink multisink_;
RpcLogDrainMap drain_map_;
std::array<std::byte, kMaxLogEntrySize> entry_encode_buffer_;
static constexpr size_t kMaxFilterRules = 3;
static constexpr size_t kMaxFilterRules = 4;
std::array<Filter::Rule, kMaxFilterRules> rules1_;
std::array<Filter::Rule, kMaxFilterRules> rules2_;
std::array<Filter::Rule, kMaxFilterRules> rules3_;
Expand Down

0 comments on commit 71b85f8

Please sign in to comment.