Skip to content

Commit

Permalink
fix(pdf): handle fs::canonical errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed Oct 3, 2024
1 parent 4b284f5 commit 1f01c64
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/pdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ namespace pdfcomp
comp.emplace_back(image, distortion);
}

const auto values = comp | std::views::values;
auto differences = std::accumulate(values.begin(), values.end(), 0.0);
const auto values = comp | std::views::values;
const auto differences = std::accumulate(values.begin(), values.end(), 0.0);

if (!output)
{
Expand Down Expand Up @@ -85,8 +85,15 @@ namespace pdfcomp
static std::once_flag flag;
std::call_once(flag, []() { Magick::InitializeMagick(fs::current_path().string().c_str()); });

auto path = fs::canonical(document);
auto data = impl{};
std::error_code ec{};
const auto path = fs::canonical(document, ec);

if (ec)
{
return tl::unexpected{error::bad_file};
}

impl data{};

try
{
Expand All @@ -97,6 +104,11 @@ namespace pdfcomp
std::println(stderr, "Error ('{}'): {}", path.string(), err.what());
return tl::unexpected{error::bad_file};
}
catch (...)
{
std::println(stderr, "Unknown error");
return tl::unexpected{error::bad_file};
}

return pdf{std::move(data)};
}
Expand Down

0 comments on commit 1f01c64

Please sign in to comment.