CMP0200: Fix crash in configuration selection policy warning

The recently introduced CMP0200 warnings could lead to a crash when
encountering targets with empty configuration values. Avoid calling
substr on an empty `string_view`.

Fixes: #27204
This commit is contained in:
Joerg Bornemann
2025-09-10 09:29:05 +02:00
committed by Brad King
parent 082071203f
commit 763aa084b0
3 changed files with 17 additions and 8 deletions

View File

@@ -3234,10 +3234,14 @@ bool cmTarget::GetMappedConfig(std::string const& desiredConfig, cmValue& loc,
bool const newResult =
this->GetMappedConfigNew(desiredConfig, newLoc, newImp, newSuffix);
auto configFromSuffix = [](cm::string_view s) -> cm::string_view {
return s.empty() ? "(none)"_s : s.substr(1);
};
if (!this->GetMappedConfigOld(desiredConfig, loc, imp, suffix)) {
if (newResult) {
// NEW policy found a configuration, OLD did not.
auto newConfig = cm::string_view{ newSuffix }.substr(1);
cm::string_view newConfig = configFromSuffix(newSuffix);
std::string const err = cmStrCat(
cmPolicies::GetPolicyWarning(cmPolicies::CMP0200),
"\nConfiguration selection for imported target \"", this->GetName(),
@@ -3249,7 +3253,7 @@ bool cmTarget::GetMappedConfig(std::string const& desiredConfig, cmValue& loc,
return false;
}
auto oldConfig = cm::string_view{ suffix }.substr(1);
cm::string_view oldConfig = configFromSuffix(suffix);
if (!newResult) {
// NEW policy did not find a configuration, OLD did.
std::string const err =
@@ -3260,7 +3264,7 @@ bool cmTarget::GetMappedConfig(std::string const& desiredConfig, cmValue& loc,
this->GetMakefile()->IssueMessage(MessageType::AUTHOR_WARNING, err);
} else if (suffix != newSuffix) {
// OLD and NEW policies found different configurations.
auto newConfig = cm::string_view{ newSuffix }.substr(1);
cm::string_view newConfig = configFromSuffix(newSuffix);
std::string const err =
cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0200),
"\nConfiguration selection for imported target \"",

View File

@@ -4,7 +4,7 @@ CMake Warning \(dev\) in CMakeLists\.txt:
policy details\. Use the cmake_policy command to set the policy and
suppress this warning\.
Configuration selection for imported target "lib_test" selected
Configuration selection for imported target "lib_test1" selected
configuration "CAT", but would select configuration "DOG" under the NEW
policy\.

View File

@@ -2,10 +2,15 @@ project(test-CMP0200-WARN C)
set(CMAKE_POLICY_WARNING_CMP0200 ON)
add_library(lib_test INTERFACE IMPORTED)
set_target_properties(lib_test PROPERTIES
add_library(lib_test1 INTERFACE IMPORTED)
set_target_properties(lib_test1 PROPERTIES
IMPORTED_CONFIGURATIONS "DOG;CAT"
)
add_executable(exe_test configtest.c)
target_link_libraries(exe_test PRIVATE lib_test)
add_executable(exe_test1 configtest.c)
target_link_libraries(exe_test1 PRIVATE lib_test1)
add_library(lib_test2 INTERFACE IMPORTED)
add_executable(exe_test2 configtest.c)
target_link_libraries(exe_test2 PRIVATE lib_test2)