Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: throw exception in debug mode that condition is always true when fractionalPercent numerator > denominator #12068

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions source/common/runtime/runtime_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ bool SnapshotImpl::featureEnabled(absl::string_view key,
percent = default_value;
}

#ifndef NDEBUG
// When numerator > denominator condition is always evaluates to TRUE
// It becomes hard to debug why configuration does not work in case of wrong numerator.
// Log debug message that numerator is invalid.
uint64_t denominator_value =
ProtobufPercentHelper::fractionalPercentDenominatorToInt(percent.denominator());
if (percent.numerator() > denominator_value) {
throw EnvoyException(fmt::format("runtime key '{}': numerator ({}) > denominator ({}), "
"condition always evaluates to true",
key, percent.numerator(), denominator_value));
}
#endif

return ProtobufPercentHelper::evaluateFractionalPercent(percent, random_value);
}

Expand Down
21 changes: 21 additions & 0 deletions test/common/runtime/runtime_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,27 @@ TEST_F(StaticLoaderImplTest, ProtoParsing) {
EXPECT_EQ(2, store_.gauge("runtime.num_layers", Stats::Gauge::ImportMode::NeverImport).value());
}

TEST_F(StaticLoaderImplTest, InvalidNumerator) {
base_ = TestUtility::parseYaml<ProtobufWkt::Struct>(R"EOF(
invalid_numerator:
numerator: 111
denominator: HUNDRED
)EOF");
setup();

envoy::type::v3::FractionalPercent fractional_percent;

// Numerator > denominator -> exception
EXPECT_CALL(generator_, random()).WillOnce(Return(500000));
ggreenway marked this conversation as resolved.
Show resolved Hide resolved
// Exception is thrown only on debug mode
#if !defined(NDEBUG)
EXPECT_THROW_WITH_MESSAGE(
loader_->snapshot().featureEnabled("invalid_numerator", fractional_percent), EnvoyException,
"runtime key 'invalid_numerator': numerator (111) > denominator (100), condition "
"always evaluates to true");
#endif
}
belyalov marked this conversation as resolved.
Show resolved Hide resolved

TEST_F(StaticLoaderImplTest, RuntimeFromNonWorkerThreads) {
// Force the thread to be considered a non-worker thread.
tls_.registered_ = false;
Expand Down