szaszm commented on code in PR #1888:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1888#discussion_r1840872884
##########
libminifi/src/core/flow/StructuredConfiguration.cpp:
##########
@@ -145,6 +145,41 @@ std::unique_ptr<core::ProcessGroup>
StructuredConfiguration::getRootFrom(const N
}
}
+namespace {
+bool hasInheritanceCycle(const ParameterContext& parameter_context,
std::unordered_set<std::string>& visited_parameter_contexts,
std::unordered_set<std::string>& current_stack) {
+ if (current_stack.contains(parameter_context.getName())) {
+ return true;
+ }
+
+ if (visited_parameter_contexts.contains(parameter_context.getName())) {
+ return false;
+ }
+
+ current_stack.insert(parameter_context.getName());
+ visited_parameter_contexts.insert(parameter_context.getName());
+
+ for (const auto& inherited_parameter_context :
parameter_context.getInheritedParameterContexts()) {
+ if (hasInheritanceCycle(*inherited_parameter_context,
visited_parameter_contexts, current_stack)) {
+ return true;
+ }
+ }
+
+ current_stack.erase(parameter_context.getName());
+
+ return false;
+}
+} // namespace
+
+void StructuredConfiguration::verifyNoInheritanceCycles() const {
+ std::unordered_set<std::string> visited_parameter_contexts;
+ std::unordered_set<std::string> current_stack;
+ for (const auto& [parameter_context_name, parameter_context] :
parameter_contexts_) {
+ if (hasInheritanceCycle(*parameter_context, visited_parameter_contexts,
current_stack)) {
+ throw std::invalid_argument("Circular references in Parameter Context
inheritance are not allowed. Inheritance cycle was detected in parameter
context '" + parameter_context->getName() + "'");
+ }
+ }
+}
+
Review Comment:
I'd add a timeout here, in the unlikely case we need to walk wide and deep
inheritance trees and find long cycles, because the complexity of this check
seems O(scary) with the recursion in `hasInheritanceCycle`.
Alternatively, we could put a hard limit on the number of contexts, or the
number of inheritance levels, so the runtime of this algorithm becomes more
predictable and manageable.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]