浏览代码

set_property: Fix crash when setting LINK_LIBRARIES to nothing

We use a special dedicated structure to store the LINK_LIBRARIES target
property.  Do not try to construct a string from a NULL value.  Instead
leave the property structure empty when no value is given.

Reported-by: Ghyslain Leclerc <[email protected]>
Brad King 10 年之前
父节点
当前提交
7aa9e80e35

+ 12 - 6
Source/cmTarget.cxx

@@ -1748,9 +1748,12 @@ void cmTarget::SetProperty(const std::string& prop, const char* value)
   else if (prop == "LINK_LIBRARIES")
     {
     this->Internal->LinkImplementationPropertyEntries.clear();
-    cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
-    cmValueWithOrigin entry(value, lfbt);
-    this->Internal->LinkImplementationPropertyEntries.push_back(entry);
+    if (value)
+      {
+      cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
+      cmValueWithOrigin entry(value, lfbt);
+      this->Internal->LinkImplementationPropertyEntries.push_back(entry);
+      }
     }
   else if (prop == "SOURCES")
     {
@@ -1834,9 +1837,12 @@ void cmTarget::AppendProperty(const std::string& prop, const char* value,
     }
   else if (prop == "LINK_LIBRARIES")
     {
-    cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
-    cmValueWithOrigin entry(value, lfbt);
-    this->Internal->LinkImplementationPropertyEntries.push_back(entry);
+    if (value)
+      {
+      cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
+      cmValueWithOrigin entry(value, lfbt);
+      this->Internal->LinkImplementationPropertyEntries.push_back(entry);
+      }
     }
   else if (prop == "SOURCES")
     {

+ 1 - 0
Tests/RunCMake/CMakeLists.txt

@@ -146,6 +146,7 @@ add_RunCMake_test(list)
 add_RunCMake_test(message)
 add_RunCMake_test(project)
 add_RunCMake_test(return)
+add_RunCMake_test(set_property)
 add_RunCMake_test(string)
 add_RunCMake_test(try_compile)
 add_RunCMake_test(try_run)

+ 3 - 0
Tests/RunCMake/set_property/CMakeLists.txt

@@ -0,0 +1,3 @@
+cmake_minimum_required(VERSION 3.2)
+project(${RunCMake_TEST} NONE)
+include(${RunCMake_TEST}.cmake)

+ 7 - 0
Tests/RunCMake/set_property/LINK_LIBRARIES.cmake

@@ -0,0 +1,7 @@
+add_custom_target(CustomTarget)
+set_property(TARGET CustomTarget PROPERTY LINK_LIBRARIES)
+set_property(TARGET CustomTarget APPEND PROPERTY LINK_LIBRARIES)
+get_property(val TARGET CustomTarget PROPERTY LINK_LIBRARIES)
+if (NOT "${val}" STREQUAL "")
+  message(FATAL_ERROR "LINK_LIBRARIES value is '${val}' but should be ''")
+endif()

+ 3 - 0
Tests/RunCMake/set_property/RunCMakeTest.cmake

@@ -0,0 +1,3 @@
+include(RunCMake)
+
+run_cmake(LINK_LIBRARIES)