Skip to content

Commit ccb42a3

Browse files
authored
Add defensive nullptr check (#44396)
The pointer can not be nullptr at this point, but add a defensive check to cover future changes. Risk Level: low Testing: unit tests Docs Changes: no Release Notes: no Platform Specific Features: no Signed-off-by: Yan Avlasov <yavlasov@google.com>
1 parent 35c73c3 commit ccb42a3

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

source/extensions/filters/listener/original_dst/original_dst.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ Network::FilterStatus OriginalDstFilter::onAccept(Network::ListenerFilterCallbac
9090
} else {
9191
const auto* local_object = cb.filterState().getDataReadOnly<Network::AddressObject>(
9292
FilterNames::get().LocalFilterStateKey);
93-
if (local_object) {
93+
if (local_object && local_object->address()) {
9494
ENVOY_LOG_MISC(debug, "original_dst: set destination from filter state to {}",
9595
local_object->address()->asString());
9696
socket.connectionInfoProvider().restoreLocalAddress(local_object->address());
9797
}
9898
}
9999
const auto* remote_object = cb.filterState().getDataReadOnly<Network::AddressObject>(
100100
FilterNames::get().RemoteFilterStateKey);
101-
if (remote_object) {
101+
if (remote_object && remote_object->address()) {
102102
ENVOY_LOG_MISC(debug, "original_dst: set source from filter state to {}",
103103
remote_object->address()->asString());
104104
socket.connectionInfoProvider().setRemoteAddress(remote_object->address());

test/extensions/filters/listener/original_dst/original_dst_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ TEST_F(OriginalDstTest, InternalFilterState) {
9898
EXPECT_EQ(remote->asString(), socket_.connectionInfoProvider().remoteAddress()->asString());
9999
}
100100

101+
TEST_F(OriginalDstTest, InternalFilterStateNullAddress) {
102+
expectInternalAddress();
103+
cb_.filter_state_.setData("envoy.filters.listener.original_dst.local_ip",
104+
std::make_shared<Network::AddressObject>(nullptr),
105+
StreamInfo::FilterState::StateType::Mutable,
106+
StreamInfo::FilterState::LifeSpan::Connection);
107+
cb_.filter_state_.setData("envoy.filters.listener.original_dst.remote_ip",
108+
std::make_shared<Network::AddressObject>(nullptr),
109+
StreamInfo::FilterState::StateType::Mutable,
110+
StreamInfo::FilterState::LifeSpan::Connection);
111+
filter_.onAccept(cb_);
112+
EXPECT_FALSE(socket_.connectionInfoProvider().localAddressRestored());
113+
}
114+
101115
} // namespace
102116
} // namespace OriginalDst
103117
} // namespace ListenerFilters

0 commit comments

Comments
 (0)