فهرست منبع

Merge topic 'get_dir_prop_arg_parsing'

ab1ee5aab8 get_directory_property: Check for empty or missing property name

Acked-by: Kitware Robot <[email protected]>
Merge-request: !5577
Brad King 4 سال پیش
والد
کامیت
e94f00dfe3

+ 25 - 18
Source/cmGetDirectoryPropertyCommand.cxx

@@ -50,6 +50,10 @@ bool cmGetDirectoryPropertyCommand(std::vector<std::string> const& args,
       return false;
     }
     ++i;
+    if (i == args.end()) {
+      status.SetError("called with incorrect number of arguments");
+      return false;
+    }
   }
 
   // OK, now we have the directory to process, we just get the requested
@@ -67,27 +71,30 @@ bool cmGetDirectoryPropertyCommand(std::vector<std::string> const& args,
     return true;
   }
 
+  if (i->empty()) {
+    status.SetError("given empty string for the property name to get");
+    return false;
+  }
+
   const char* prop = nullptr;
-  if (!i->empty()) {
-    if (*i == "DEFINITIONS") {
-      switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0059)) {
-        case cmPolicies::WARN:
-          status.GetMakefile().IssueMessage(
-            MessageType::AUTHOR_WARNING,
-            cmPolicies::GetPolicyWarning(cmPolicies::CMP0059));
-          CM_FALLTHROUGH;
-        case cmPolicies::OLD:
-          StoreResult(status.GetMakefile(), variable,
-                      status.GetMakefile().GetDefineFlagsCMP0059());
-          return true;
-        case cmPolicies::NEW:
-        case cmPolicies::REQUIRED_ALWAYS:
-        case cmPolicies::REQUIRED_IF_USED:
-          break;
-      }
+  if (*i == "DEFINITIONS") {
+    switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0059)) {
+      case cmPolicies::WARN:
+        status.GetMakefile().IssueMessage(
+          MessageType::AUTHOR_WARNING,
+          cmPolicies::GetPolicyWarning(cmPolicies::CMP0059));
+        CM_FALLTHROUGH;
+      case cmPolicies::OLD:
+        StoreResult(status.GetMakefile(), variable,
+                    status.GetMakefile().GetDefineFlagsCMP0059());
+        return true;
+      case cmPolicies::NEW:
+      case cmPolicies::REQUIRED_ALWAYS:
+      case cmPolicies::REQUIRED_IF_USED:
+        break;
     }
-    prop = cmToCStr(dir->GetProperty(*i));
   }
+  prop = cmToCStr(dir->GetProperty(*i));
   StoreResult(status.GetMakefile(), variable, prop);
   return true;
 }

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

@@ -2,6 +2,9 @@ include(RunCMake)
 
 run_cmake(cache_properties)
 run_cmake(directory_properties)
+run_cmake(get_directory_property_empty)
+run_cmake(get_directory_property_missing)
+run_cmake(get_directory_property_missingWithDir)
 run_cmake(global_properties)
 run_cmake(install_properties)
 run_cmake(source_properties)

+ 1 - 0
Tests/RunCMake/get_property/get_directory_property_empty-result.txt

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

+ 4 - 0
Tests/RunCMake/get_property/get_directory_property_empty-stderr.txt

@@ -0,0 +1,4 @@
+^CMake Error at get_directory_property_empty.cmake:1 \(get_directory_property\):
+  get_directory_property given empty string for the property name to get
+Call Stack \(most recent call first\):
+  CMakeLists\.txt:[0-9]+ \(include\)

+ 1 - 0
Tests/RunCMake/get_property/get_directory_property_empty.cmake

@@ -0,0 +1 @@
+get_directory_property(outVar "")

+ 1 - 0
Tests/RunCMake/get_property/get_directory_property_missing-result.txt

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

+ 4 - 0
Tests/RunCMake/get_property/get_directory_property_missing-stderr.txt

@@ -0,0 +1,4 @@
+^CMake Error at get_directory_property_missing.cmake:1 \(get_directory_property\):
+  get_directory_property called with incorrect number of arguments
+Call Stack \(most recent call first\):
+  CMakeLists\.txt:[0-9]+ \(include\)

+ 1 - 0
Tests/RunCMake/get_property/get_directory_property_missing.cmake

@@ -0,0 +1 @@
+get_directory_property(outVar)

+ 1 - 0
Tests/RunCMake/get_property/get_directory_property_missingWithDir-result.txt

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

+ 4 - 0
Tests/RunCMake/get_property/get_directory_property_missingWithDir-stderr.txt

@@ -0,0 +1,4 @@
+^CMake Error at get_directory_property_missingWithDir.cmake:1 \(get_directory_property\):
+  get_directory_property called with incorrect number of arguments
+Call Stack \(most recent call first\):
+  CMakeLists\.txt:[0-9]+ \(include\)

+ 1 - 0
Tests/RunCMake/get_property/get_directory_property_missingWithDir.cmake

@@ -0,0 +1 @@
+get_directory_property(outVar DIRECTORY .)