Skip to content

Commit 1c2947b

Browse files
fix(relative-paths): Prevent processing paths which may contain CMake macros, added a check to see if the library that's being linked exists on disk
1 parent 19e61aa commit 1c2947b

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/project_parser.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -598,21 +598,37 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
598598
t.optional("private-link-libraries", target.private_link_libraries);
599599

600600
// Add support for relative paths for (private-)link-libraries
601-
const auto fix_relative_paths = [](ConditionVector &libraries) {
601+
const auto fix_relative_paths = [&name, &path](ConditionVector &libraries, const char *key) {
602602
for (const auto &library_entries : libraries) {
603-
for (auto &path : libraries[library_entries.first]) {
603+
for (auto &library_path : libraries[library_entries.first]) {
604+
// Skip processing paths with potential CMake macros in them (this check isn't perfect)
605+
// https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#variable-references
606+
if ((library_path.find("${") != std::string::npos || library_path.find("$ENV{") != std::string::npos ||
607+
library_path.find("$CACHE{") != std::string::npos) &&
608+
library_path.find('}') != std::string::npos) {
609+
continue;
610+
}
611+
604612
// Skip absolute paths and paths which do not include a parent path
605-
const fs::path library_file_path {path};
606-
if (library_file_path.is_absolute() || !library_file_path.has_parent_path())
613+
const fs::path library_file_path{library_path};
614+
if (library_file_path.is_absolute() || !library_file_path.has_parent_path()) {
607615
continue;
616+
}
617+
618+
// Check if the new file path exists, otherwise emit an error
619+
const auto expected_library_file_path = fs::path{path} / library_file_path;
620+
if (!fs::exists(expected_library_file_path)) {
621+
throw std::runtime_error("Attempted to link against a library file that doesn't exist for target \"" + name + "\" in \"" +
622+
key + "\": " + library_path);
623+
}
608624

609625
// Prepend ${CMAKE_CURRENT_SOURCE_DIR} to the path
610-
path.insert(0, "${CMAKE_CURRENT_SOURCE_DIR}/");
626+
library_path.insert(0, "${CMAKE_CURRENT_SOURCE_DIR}/");
611627
}
612628
}
613629
};
614-
fix_relative_paths(target.link_libraries);
615-
fix_relative_paths(target.private_link_libraries);
630+
fix_relative_paths(target.link_libraries, "link-libraries");
631+
fix_relative_paths(target.private_link_libraries, "private-link-libraries");
616632

617633
t.optional("link-options", target.link_options);
618634
t.optional("private-link-options", target.private_link_options);

0 commit comments

Comments
 (0)