Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 46 additions & 35 deletions src/build/adapter_filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#include <sourcemeta/core/io.h>

#include <cassert> // assert
#include <fstream> // std::ofstream
#include <fstream> // std::ofstream, std::ifstream
#include <mutex> // std::unique_lock
#include <string> // std::string
#include <string_view> // std::string_view

namespace sourcemeta::one {
Expand All @@ -25,45 +26,55 @@ auto BuildAdapterFilesystem::read_dependencies(const node_type &path) const
-> std::optional<BuildDependencies<node_type>> {
assert(path.is_absolute());
const auto dependencies_path{this->dependencies_path(path)};
if (std::filesystem::exists(dependencies_path)) {
auto stream{sourcemeta::core::read_file(dependencies_path)};
assert(stream.is_open());

BuildDependencies<node_type> result;
std::string line;
while (std::getline(stream, line)) {
// Prevent CRLF on Windows
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}

if (!line.empty()) {
auto kind{BuildDependencyKind::Static};
std::filesystem::path dependency;
if (line.size() >= 2 && line[1] == ' ' &&
(line[0] == 's' || line[0] == 'd')) {
kind = (line[0] == 'd') ? BuildDependencyKind::Dynamic
: BuildDependencyKind::Static;
dependency = line.substr(2);
} else {
dependency = line;
}
if (!dependency.is_absolute()) {
dependency =
std::filesystem::weakly_canonical(this->root / dependency);
}

result.emplace_back(kind, std::move(dependency));
}
std::ifstream stream{dependencies_path};
if (!stream.is_open()) {
return std::nullopt;
}

std::string contents{std::istreambuf_iterator<char>(stream),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After building contents from the stream, there’s no check for read errors (e.g., badbit/failbit), so a partial read could yield an incomplete dependency list and make incremental builds incorrectly treat cached dependencies as valid.

Severity: medium

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

std::istreambuf_iterator<char>()};

BuildDependencies<node_type> result;
std::size_t position{0};
while (position < contents.size()) {
auto newline{contents.find('\n', position)};
if (newline == std::string::npos) {
newline = contents.size();
}

if (result.empty()) {
return std::nullopt;
} else {
return result;
auto end{newline};
// Prevent CRLF on Windows
if (end > position && contents[end - 1] == '\r') {
end -= 1;
}
} else {

if (end > position) {
auto kind{BuildDependencyKind::Static};
std::filesystem::path dependency;
const auto length{end - position};
if (length >= 2 && contents[position + 1] == ' ' &&
(contents[position] == 's' || contents[position] == 'd')) {
kind = (contents[position] == 'd') ? BuildDependencyKind::Dynamic
: BuildDependencyKind::Static;
dependency = contents.substr(position + 2, end - position - 2);
} else {
dependency = contents.substr(position, length);
}
if (!dependency.is_absolute()) {
dependency = (this->root / dependency).lexically_normal();
}

result.emplace_back(kind, std::move(dependency));
}

position = newline + 1;
}

if (result.empty()) {
return std::nullopt;
} else {
return result;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/index/index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,9 @@ static auto index_main(const std::string_view &program,
const auto relative_path{std::filesystem::relative(entry, schemas_path)};
print_progress(mutex, 1, "Producing", relative_path.string(), cursor + 1,
directories.size());
const auto destination{std::filesystem::weakly_canonical(
explorer_path / relative_path / SENTINEL / "directory.metapack")};
const auto destination{
(explorer_path / relative_path / SENTINEL / "directory.metapack")
.lexically_normal()};
DISPATCH<sourcemeta::one::GENERATE_EXPLORER_DIRECTORY_LIST>(
destination,
// If any of the entry summary files changes, by definition we need to
Expand Down
3 changes: 1 addition & 2 deletions src/index/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ namespace sourcemeta::one {

class Output {
public:
Output(std::filesystem::path path)
: path_{std::filesystem::weakly_canonical(path)} {
Output(std::filesystem::path path) : path_{path.lexically_normal()} {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output now stores path_ via lexically_normal() without ensuring it’s absolute; track() later walks parent_path() until it equals this->path_, which can become non-terminating at filesystem root if path_ is relative.

Severity: medium

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

std::filesystem::create_directories(this->path_);
for (const auto &entry :
std::filesystem::recursive_directory_iterator(this->path_)) {
Expand Down