find_package: Mismatched root path stack push-pop when FetchContent override fails
If a FetchContent_Declare()
call includes the OVERRIDE_FIND_PACKAGE
keyword, it will redirect calls to find_package()
for that dependency to FetchContent_MakeAvailable()
. Any call to FetchContent_MakeAvailable()
must succeed, or it will generate a fatal error. When a fatal error occurs, the C++ implementation has an early return
in the code here. Refactoring in c0fcd07e for !8240 (merged) collapsed some code to a single call to PushFindPackageRootPathStack()
before this, expecting an associated call to PopFindPackageRootPathStack()
towards the end of the function (also pre-existing, but cleaned up in the same refactoring), but the early return means the pop is not reached. There may be a similar problem with SetModuleVariables()
and RestoreFindDefinitions()
.
Given that this only occurs if there has already been a fatal error, execution should not continue beyond the failing call. The actual state of variables shouldn't really matter at that point. But to prevent potential leaks or other inconsistencies in future work, it would be good to avoid returning from the function with inconsistent state. Use of a RAII mechanism may be the more robust way to handle this.