Procházet zdrojové kódy

Genex: Diagnose invalid LINK_ONLY usage instead of crashing

When `$<LINK_ONLY:...>` is used outside of linking we may evaluate it
without a `dagChecker`.  Do not dereference the NULL pointer and issue a
diagnostic instead.

Closes: #16287
Brad King před 9 roky
rodič
revize
f6fd0abc5b

+ 8 - 3
Source/cmGeneratorExpressionNode.cxx

@@ -672,15 +672,20 @@ static const struct LinkOnlyNode : public cmGeneratorExpressionNode
   LinkOnlyNode() {}
 
   std::string Evaluate(const std::vector<std::string>& parameters,
-                       cmGeneratorExpressionContext* /*context*/,
-                       const GeneratorExpressionContent* /*content*/,
+                       cmGeneratorExpressionContext* context,
+                       const GeneratorExpressionContent* content,
                        cmGeneratorExpressionDAGChecker* dagChecker) const
     CM_OVERRIDE
   {
+    if (!dagChecker) {
+      reportError(context, content->GetOriginalExpression(),
+                  "$<LINK_ONLY:...> may only be used for linking");
+      return std::string();
+    }
     if (!dagChecker->GetTransitivePropertiesOnly()) {
       return parameters.front();
     }
-    return "";
+    return std::string();
   }
 } linkOnlyNode;
 

+ 1 - 0
Tests/RunCMake/GeneratorExpression/LINK_ONLY-not-linking-result.txt

@@ -0,0 +1 @@
+1

+ 8 - 0
Tests/RunCMake/GeneratorExpression/LINK_ONLY-not-linking-stderr.txt

@@ -0,0 +1,8 @@
+CMake Error at LINK_ONLY-not-linking.cmake:1 \(add_custom_target\):
+  Error evaluating generator expression:
+
+    \$<LINK_ONLY:something>
+
+  \$<LINK_ONLY:...> may only be used for linking
+Call Stack \(most recent call first\):
+  CMakeLists.txt:3 \(include\)

+ 1 - 0
Tests/RunCMake/GeneratorExpression/LINK_ONLY-not-linking.cmake

@@ -0,0 +1 @@
+add_custom_target(Custom ALL COMMAND ${CMAKE_COMMAND} -E echo $<LINK_ONLY:something>)

+ 1 - 0
Tests/RunCMake/GeneratorExpression/RunCMakeTest.cmake

@@ -29,6 +29,7 @@ run_cmake(COMPILE_LANGUAGE-unknown-lang)
 run_cmake(TARGET_FILE-recursion)
 run_cmake(OUTPUT_NAME-recursion)
 run_cmake(TARGET_PROPERTY-LOCATION)
+run_cmake(LINK_ONLY-not-linking)
 
 run_cmake(ImportedTarget-TARGET_PDB_FILE)
 if(LINKER_SUPPORTS_PDB)