| 67 | } |
| 68 | |
| 69 | static std::vector<fs::path> expand_cmake_path(const fs::path &source_path, const fs::path &toml_dir, bool is_root_project) { |
| 70 | auto is_subdir = [](fs::path p, const fs::path &root) { |
| 71 | while (true) { |
| 72 | if (p == root) { |
| 73 | return true; |
| 74 | } |
| 75 | auto parent = p.parent_path(); |
| 76 | if (parent == p) { |
| 77 | break; |
| 78 | } |
| 79 | p = parent; |
| 80 | } |
| 81 | return false; |
| 82 | }; |
| 83 | if (!is_subdir(fs::absolute(toml_dir / source_path), toml_dir)) { |
| 84 | throw std::runtime_error("Path traversal is not allowed: " + source_path.string()); |
| 85 | } |
| 86 | |
| 87 | // Split the path at the first period (since fs::path::stem() and fs::path::extension() split at the last period) |
| 88 | std::string stem, extension; |
| 89 | auto filename = source_path.filename().string(); |
| 90 | auto dot_position = filename.find('.'); |
| 91 | if (dot_position != std::string::npos) { |
| 92 | stem = filename.substr(0, dot_position); |
| 93 | extension = filename.substr(dot_position); |
| 94 | } else { |
| 95 | stem = filename; |
| 96 | } |
| 97 | |
| 98 | if (is_root_project && stem == "**" && !source_path.has_parent_path()) { |
| 99 | throw std::runtime_error("Recursive globbing not allowed in project root: " + source_path.string()); |
| 100 | } |
| 101 | |
| 102 | auto has_extension = [](const fs::path &file_path, const std::string &extension) { |
| 103 | auto path = file_path.string(); |
| 104 | return path.rfind(extension) == path.length() - extension.length(); |
| 105 | }; |
| 106 | |
| 107 | std::vector<fs::path> paths; |
| 108 | if (stem == "*") { |
| 109 | for (const auto &f : fs::directory_iterator(toml_dir / source_path.parent_path(), fs::directory_options::follow_directory_symlink)) { |
| 110 | if (!f.is_directory() && has_extension(f.path(), extension)) { |
| 111 | paths.push_back(fs::relative(f, toml_dir)); |
| 112 | } |
| 113 | } |
| 114 | } else if (stem == "**") { |
| 115 | for (const auto &f : |
| 116 | fs::recursive_directory_iterator(toml_dir / source_path.parent_path(), fs::directory_options::follow_directory_symlink)) { |
| 117 | if (!f.is_directory() && has_extension(f.path(), extension)) { |
| 118 | paths.push_back(fs::relative(f, toml_dir)); |
| 119 | } |
| 120 | } |
| 121 | } else { |
| 122 | paths.push_back(source_path); |
| 123 | } |
| 124 | |
| 125 | return paths; |
| 126 | } |
no outgoing calls
no test coverage detected