summaryrefslogtreecommitdiffstats
path: root/Source/cmFindPackageCommand.cxx
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2023-03-13 15:07:55 (GMT)
committerBrad King <brad.king@kitware.com>2023-03-13 15:48:19 (GMT)
commit60ef076bacca9a265ab4ab68032933c464e84a66 (patch)
tree402214fe98f06615b8f2f1dc21299649da5bd9ec /Source/cmFindPackageCommand.cxx
parent89b69bf1addad6138b9aa51c2f67380c9e489f2f (diff)
downloadCMake-60ef076bacca9a265ab4ab68032933c464e84a66.zip
CMake-60ef076bacca9a265ab4ab68032933c464e84a66.tar.gz
CMake-60ef076bacca9a265ab4ab68032933c464e84a66.tar.bz2
find_package: Enforce maximum nesting depth below maximum recursion depth
The stack usage for nested `find_package` calls is much larger than for other kinds of recursion, so enforce a lower limit to avoid stack overflow.
Diffstat (limited to 'Source/cmFindPackageCommand.cxx')
-rw-r--r--Source/cmFindPackageCommand.cxx15
1 files changed, 15 insertions, 0 deletions
diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx
index 8acfe83..b1e65af 100644
--- a/Source/cmFindPackageCommand.cxx
+++ b/Source/cmFindPackageCommand.cxx
@@ -976,6 +976,21 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
}
}
+ // Limit package nesting depth well below the recursion depth limit because
+ // find_package nesting uses more stack space than normal recursion.
+ {
+ static std::size_t const findPackageDepthMinMax = 100;
+ std::size_t const findPackageDepthMax = std::max(
+ this->Makefile->GetRecursionDepthLimit() / 2, findPackageDepthMinMax);
+ std::size_t const findPackageDepth =
+ this->Makefile->FindPackageRootPathStack.size() + 1;
+ if (findPackageDepth > findPackageDepthMax) {
+ this->SetError(cmStrCat("maximum nesting depth of ", findPackageDepthMax,
+ " exceeded."));
+ return false;
+ }
+ }
+
this->PushFindPackageRootPathStack();
this->SetModuleVariables(components, componentVarDefs);