Browse Source

cmSystemTools: Fix SplitEnvPath to avoid empty paths

Brad King 1 year ago
parent
commit
6d57403e14
2 changed files with 17 additions and 3 deletions
  1. 15 2
      Source/cmSystemTools.cxx
  2. 2 1
      Source/cmSystemTools.h

+ 15 - 2
Source/cmSystemTools.cxx

@@ -1614,14 +1614,27 @@ cm::optional<std::string> cmSystemTools::GetEnvVar(std::string const& var)
   return result;
 }
 
-std::vector<std::string> cmSystemTools::SplitEnvPath(std::string const& value)
+std::vector<std::string> cmSystemTools::SplitEnvPath(cm::string_view in)
 {
 #if defined(_WIN32) && !defined(__CYGWIN__)
   static cm::string_view sep = ";"_s;
 #else
   static cm::string_view sep = ":"_s;
 #endif
-  std::vector<std::string> paths = cmTokenize(value, sep);
+  std::vector<std::string> paths;
+  cm::string_view::size_type e = 0;
+  for (;;) {
+    cm::string_view::size_type b = in.find_first_not_of(sep, e);
+    if (b == cm::string_view::npos) {
+      break;
+    }
+    e = in.find_first_of(sep, b);
+    if (e == cm::string_view::npos) {
+      paths.emplace_back(in.substr(b));
+      break;
+    }
+    paths.emplace_back(in.substr(b, e - b));
+  }
   for (std::string& p : paths) {
     SystemTools::ConvertToUnixSlashes(p);
   }

+ 2 - 1
Source/cmSystemTools.h

@@ -399,7 +399,8 @@ public:
                                      std::string const& in);
 
   static cm::optional<std::string> GetEnvVar(std::string const& var);
-  static std::vector<std::string> SplitEnvPath(std::string const& value);
+
+  static std::vector<std::string> SplitEnvPath(cm::string_view in);
 
   static std::string ToNormalizedPathOnDisk(std::string p);