Repository navigation
fix(clang-tidy): use UNREACHABLE for unreachable paths (RULE-0-0-2) #1352
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,7 +140,10 @@ std::ptrdiff_t SubtractPointersBytes(const void* const first, const void* const | |
| score::mw::log::LogFatal("shm") | ||
| << "Could not subtract " << second_address_as_integer << "from" << first_address_as_integer | ||
| << ". Result does not fit into std::ptrdiff_t. Terminating."; | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(false); | ||
| // Reachable in principle, but must not be reached in production: Two addresses further apart than | ||
| // std::ptrdiff_t max are possible in principle. Shared memory regions are far smaller, so such a pair of | ||
| // addresses must not occur in production. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
| } | ||
| return static_cast<std::ptrdiff_t>(result_as_integer); | ||
| } | ||
|
|
@@ -162,7 +165,10 @@ std::ptrdiff_t SubtractPointersBytes(const void* const first, const void* const | |
| score::mw::log::LogFatal("shm") << "Could not subtract " << second_address_as_integer << "from" | ||
| << first_address_as_integer | ||
| << ". Result does not fit into std::ptrdiff_t. Terminating."; | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(false); | ||
| // Reachable in principle, but must not be reached in production: Two addresses further apart than | ||
| // std::ptrdiff_t max are possible in principle. Shared memory regions are far smaller, so such a pair of | ||
| // addresses must not occur in production. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
| } | ||
| return -1 * static_cast<std::ptrdiff_t>(absolute_value_result_as_integer); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,12 +55,16 @@ OfferedStateMachine::State StopOfferedState::Offer() | |
|
|
||
| OfferedStateMachine::State StopOfferedState::StopOffer() | ||
| { | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(0); | ||
| // Reachable in principle, but must not be reached in production: StopOffer() can be called while in | ||
| // StopOfferedState. The Skeleton must not call StopOffer() twice without an Offer() in between in production. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this unreachable? You can call StopOffer when in StopOfferedState |
||
| } | ||
|
|
||
| OfferedStateMachine::State ReOfferedState::Offer() | ||
| { | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(0); | ||
| // Reachable in principle, but must not be reached in production: Offer() can be called while in ReOfferedState. The | ||
| // Skeleton must not call Offer() twice without a StopOffer() in between in production. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
| } | ||
|
|
||
| OfferedStateMachine::State ReOfferedState::StopOffer() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,11 +64,13 @@ void EmitControlFileName(std::ostream& out, | |
| [[fallthrough]]; | ||
| default: | ||
| { | ||
| // Reachable in principle, but must not be reached in production: An invalid QualityType can be passed in. | ||
| // Callers must pass QM or ASIL-B in production. | ||
| // Suppress "AUTOSAR C++14 M0-1-1", The rule states: "A project shall not contain unreachable code" | ||
| // This is a false positive, this code is reachable due to fallthrough. | ||
| // default case and break are necessary to have well-formed switch. | ||
| // coverity[autosar_cpp14_m0_1_1_violation : FALSE] | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE(false, "Invalid quality type"); | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE_MESSAGE("Invalid quality type"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is contradicting the comment above |
||
|
|
||
| // Rationale: This code is actually unreachable due to the assert above which will always be triggered if | ||
| // hit. However, rule M6-4-5 says that "An unconditional throw or break statement shall terminate every | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,10 @@ auto GetValueFromJson(const score::json::Object& json_object, std::string_view k | |
| { | ||
| score::mw::log::LogFatal("lola") << "Failed to parse JSON configuration key '" << key | ||
| << "'. Configuration parsing failed. Terminating."; | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(false); | ||
| // Reachable in principle, but must not be reached in production: The JSON value can have a type other than T if | ||
| // the configuration file is malformed. A configuration that passed the schema validation must not reach this in | ||
| // production. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this unreachable? In what cases can json_result not return a value? |
||
| } | ||
| return json_result.value(); | ||
| } | ||
|
|
@@ -71,7 +74,10 @@ auto GetValueFromJson(const score::json::Object& json_object, std::string_view k | |
| { | ||
| score::mw::log::LogFatal("lola") << "Failed to parse JSON configuration key '" << key | ||
| << "'. Configuration parsing failed. Terminating."; | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(false); | ||
| // Reachable in principle, but must not be reached in production: The JSON value can have a type other than T if | ||
| // the configuration file is malformed. A configuration that passed the schema validation must not reach this in | ||
| // production. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this unreachable? In what cases can json_result not return a value? |
||
| } | ||
| return json_result.value(); | ||
| } | ||
|
|
@@ -87,7 +93,10 @@ auto GetValueFromJson(const score::json::Object& json_object, std::string_view k | |
| { | ||
| score::mw::log::LogFatal("lola") << "Failed to parse JSON configuration key '" << key | ||
| << "'. Configuration parsing failed. Terminating."; | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(false); | ||
| // Reachable in principle, but must not be reached in production: The JSON value can have a type other than | ||
| // std::string_view if the configuration file is malformed. A configuration that passed the schema validation | ||
| // must not reach this in production. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this unreachable? In what cases can json_result not return a value? |
||
| } | ||
| return json_result.value(); | ||
| } | ||
|
|
@@ -107,7 +116,10 @@ auto GetOptionalValueFromJson(const score::json::Object& json_object, std::strin | |
| return ResultToOptionalOrElse(val_candidate->second.As<element_type>(), [key](auto) { | ||
| score::mw::log::LogFatal("lola") << "provided key" << key | ||
| << "contains a value that can not be parsed. Terminating.\n"; | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(false); | ||
| // Reachable in principle, but must not be reached in production: The JSON value can have a type other than the | ||
| // requested one if the configuration file is malformed. A configuration that passed the schema validation must | ||
| // not reach this in production. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this unreachable? In what cases can val_candidate->second.As<element_type>() not return a value? |
||
| }); | ||
| } | ||
|
|
||
|
|
@@ -155,7 +167,10 @@ auto DeserializeVariant(const score::json::Object& /* json_object */, | |
| std::ptrdiff_t /* variant_index */, | ||
| std::string_view /* json_variant_key */) -> VariantType | ||
| { | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(false); | ||
| // Reachable in principle, but must not be reached in production: VariantIndex can reach the number of alternatives | ||
| // if the deserialized variant index is out of range. A configuration that passed the schema validation must not | ||
| // reach this in production. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need a comment as to why this is unreachable |
||
| } | ||
|
|
||
| template <std::size_t VariantIndex, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,7 +127,10 @@ auto LolaEventInstanceDeployment::GetNumberOfSampleSlots() const -> std::optiona | |
| { | ||
| ::score::mw::log::LogFatal("lola") | ||
| << "Number of sample slots + number of tracing slots exceeds sample slot limit. Terminating."; | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(false); | ||
| // Reachable in principle, but must not be reached in production: The sum of sample slots and tracing slots can | ||
| // exceed the 16 bit limit for a misconfigured deployment. A configuration validated for production must not | ||
| // exceed it. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this unreachable? |
||
| } | ||
| return static_cast<std::uint16_t>(intermediate); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,7 +49,10 @@ QualityType GetQualityTypeFromJson(const score::json::Object& json_object, std:: | |
| { | ||
| score::mw::log::LogFatal("lola") << "Failed to parse JSON configuration key '" << key | ||
| << "' to string type. Configuration parsing failed. Terminating."; | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(false); | ||
| // Reachable in principle, but must not be reached in production: The JSON value can be no string if the | ||
| // configuration file is malformed. A configuration that passed the schema validation must not reach this in | ||
| // production. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this unreachable? In what cases can json_result not return a value? |
||
| } | ||
| return FromString(json_result.value()); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,8 +55,10 @@ LolaMethodInstanceDeployment::QueueSize GetQueueSize(const HandleType& parent_ha | |
| if (!contains_method) | ||
| { | ||
| mw::log::LogFatal("lola") << "Provided a method name which can not be found in LolaServiceInstanceDeployment"; | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE( | ||
| false, "Provided a method name which can not be found in LolaServiceInstanceDeployment"); | ||
| // Reachable in principle, but must not be reached in production: A method name can be passed that is not part | ||
| // of the deployment. In production, method names come from the generated proxy and are part of the deployment. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE_MESSAGE( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this unreachable? |
||
| "Provided a method name which can not be found in LolaServiceInstanceDeployment"); | ||
| } | ||
| const auto& lola_method_instance_deployment = method_it->second; | ||
|
|
||
|
|
@@ -65,8 +67,10 @@ LolaMethodInstanceDeployment::QueueSize GetQueueSize(const HandleType& parent_ha | |
| { | ||
| mw::log::LogFatal("lola") | ||
| << "ProxyMethod can not be created if queue_size is not configured on the proxy side."; | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE( | ||
| false, "ProxyMethod can not be created if queue_size is not configured on the proxy side."); | ||
| // Reachable in principle, but must not be reached in production: A deployment without queue_size for a proxy | ||
| // method is possible in principle. A deployment validated for production always configures it. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE_MESSAGE( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this unreachable? |
||
| "ProxyMethod can not be created if queue_size is not configured on the proxy side."); | ||
| } | ||
| return lola_method_instance_deployment.queue_size_.value(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -128,7 +128,9 @@ void Runtime::Initialize(const runtime::RuntimeConfiguration& runtime_configurat | |
| if (!validation_result.has_value()) | ||
| { | ||
| ::score::mw::log::LogFatal("lola") << validation_result.error().UserMessage(); | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(false); | ||
| // Reachable in principle, but must not be reached in production: The configuration can fail validation. A | ||
| // configuration deployed to production must pass the validation. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the Validate doing if it can never return an emply result? |
||
| } | ||
| score::cpp::ignore = initialization_config_.emplace(std::move(config)); | ||
| } | ||
|
|
@@ -297,7 +299,9 @@ Result<void> Runtime::MergeAdditionalConfiguration(const Configuration& addition | |
| if (!validation_result.has_value()) | ||
| { | ||
| ::score::mw::log::LogFatal("lola") << validation_result.error().UserMessage(); | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(false); | ||
| // Reachable in principle, but must not be reached in production: The merged configuration can fail validation. | ||
| // A configuration deployed to production must pass the validation. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the Validate doing if it can never return an emply result? |
||
| } | ||
|
|
||
| return {}; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,9 @@ SampleAllocateeTracker::~SampleAllocateeTracker() noexcept | |
| { | ||
| score::mw::log::LogFatal("lola") << "SampleAllocateeTracker destroyed while still holding" << num_allocated | ||
| << "outstanding SampleAllocateePtr instance(s), terminating."; | ||
| SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(false); | ||
| // Reachable in principle, but must not be reached in production: A SampleAllocateePtr can outlive its owner. In | ||
| // production all SampleAllocateePtr instances must be released before the tracker is destroyed. | ||
| SCORE_LANGUAGE_FUTURECPP_UNREACHABLE(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this unreachable? |
||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this unreachable? std::ptrdiff_t is signed while std::uintptr_t is unsigned. So you could theoretically have 2 pointers which are further than std::ptrdiff_t::max apart. I think that this is possible in code and also we should be able to write a test for this.