浏览代码

cmCTestRunTest: fix modifying non-existent envvars

When appending or modifying to a variable that has not been modified
before, the iterator was not valid, but it was used to insert into the
map again. Instead, just use indexing into the map since we know it will
exist by the end of the function anyways.

Fixes: #22796
Ben Boeckel 4 年之前
父节点
当前提交
69f95cf1d9
共有 3 个文件被更改,包括 27 次插入7 次删除
  1. 2 6
      Source/CTest/cmCTestRunTest.cxx
  2. 18 0
      Tests/Environment/CMakeLists.txt
  3. 7 1
      Tests/Environment/check_mod.cmake

+ 2 - 6
Source/CTest/cmCTestRunTest.cxx

@@ -800,13 +800,9 @@ bool cmCTestRunTest::ForkProcess(
     auto apply_diff =
     auto apply_diff =
       [&env_application](const std::string& name,
       [&env_application](const std::string& name,
                          std::function<void(std::string&)> const& apply) {
                          std::function<void(std::string&)> const& apply) {
-        auto entry = env_application.find(name);
-        std::string output;
-        if (entry != env_application.end() && entry->second) {
-          output = *entry->second;
-        }
+        std::string output = env_application[name].value_or(std::string{});
         apply(output);
         apply(output);
-        entry->second = output;
+        env_application[name] = output;
       };
       };
 
 
     bool err_occurred = false;
     bool err_occurred = false;

+ 18 - 0
Tests/Environment/CMakeLists.txt

@@ -45,6 +45,12 @@ set_property(TEST EchoEnvironment3
     "STRING_MANIP=string_append:suffix"
     "STRING_MANIP=string_append:suffix"
     "STRING_MANIP=string_prepend:prefix"
     "STRING_MANIP=string_prepend:prefix"
 
 
+    # String manipulation on non-existent.
+    "STRING_DNE=string_append:post-"
+    "STRING_DNE=string_prepend:-pre"
+    "STRING_DNE=string_append:suffix"
+    "STRING_DNE=string_prepend:prefix"
+
     # Path manipulation.
     # Path manipulation.
     "PATH_MANIP=set:core"
     "PATH_MANIP=set:core"
     "PATH_MANIP=path_list_append:post"
     "PATH_MANIP=path_list_append:post"
@@ -52,10 +58,22 @@ set_property(TEST EchoEnvironment3
     "PATH_MANIP=path_list_append:suffix"
     "PATH_MANIP=path_list_append:suffix"
     "PATH_MANIP=path_list_prepend:prefix"
     "PATH_MANIP=path_list_prepend:prefix"
 
 
+    # Path manipulation on non-existent.
+    "PATH_DNE=path_list_append:post"
+    "PATH_DNE=path_list_prepend:pre"
+    "PATH_DNE=path_list_append:suffix"
+    "PATH_DNE=path_list_prepend:prefix"
+
     # CMake list manipulation.
     # CMake list manipulation.
     "CMAKE_LIST_MANIP=set:core"
     "CMAKE_LIST_MANIP=set:core"
     "CMAKE_LIST_MANIP=cmake_list_append:post"
     "CMAKE_LIST_MANIP=cmake_list_append:post"
     "CMAKE_LIST_MANIP=cmake_list_prepend:pre"
     "CMAKE_LIST_MANIP=cmake_list_prepend:pre"
     "CMAKE_LIST_MANIP=cmake_list_append:suffix"
     "CMAKE_LIST_MANIP=cmake_list_append:suffix"
     "CMAKE_LIST_MANIP=cmake_list_prepend:prefix"
     "CMAKE_LIST_MANIP=cmake_list_prepend:prefix"
+
+    # CMake list manipulation on non-existent.
+    "CMAKE_LIST_DNE=cmake_list_append:post"
+    "CMAKE_LIST_DNE=cmake_list_prepend:pre"
+    "CMAKE_LIST_DNE=cmake_list_append:suffix"
+    "CMAKE_LIST_DNE=cmake_list_prepend:prefix"
 )
 )

+ 7 - 1
Tests/Environment/check_mod.cmake

@@ -20,12 +20,18 @@ set(expect_DIRECT "new")
 set(expect_STRING_MANIP "prefix-pre-core-post-suffix")
 set(expect_STRING_MANIP "prefix-pre-core-post-suffix")
 set(expect_PATH_MANIP "prefix${path_sep}pre${path_sep}core${path_sep}post${path_sep}suffix")
 set(expect_PATH_MANIP "prefix${path_sep}pre${path_sep}core${path_sep}post${path_sep}suffix")
 set(expect_CMAKE_LIST_MANIP "prefix;pre;core;post;suffix")
 set(expect_CMAKE_LIST_MANIP "prefix;pre;core;post;suffix")
+set(expect_STRING_DNE "prefix-prepost-suffix")
+set(expect_PATH_DNE "prefix${path_sep}pre${path_sep}post${path_sep}suffix")
+set(expect_CMAKE_LIST_DNE "prefix;pre;post;suffix")
 
 
 set(expected_vars
 set(expected_vars
   DIRECT
   DIRECT
   STRING_MANIP
   STRING_MANIP
   PATH_MANIP
   PATH_MANIP
-  CMAKE_LIST_MANIP)
+  CMAKE_LIST_MANIP
+  STRING_DNE
+  PATH_DNE
+  CMAKE_LIST_DNE)
 
 
 while (out)
 while (out)
   string(FIND "${out}" "\n" nl_pos)
   string(FIND "${out}" "\n" nl_pos)