Skip to content

Commit

Permalink
Add exception guard to ReadFileCallback in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Apr 22, 2017
1 parent aa44166 commit b7951be
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions solc/CommandLineInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,32 +618,43 @@ bool CommandLineInterface::processInput()
{
ReadFile::Callback fileReader = [this](string const& _path)
{
auto path = boost::filesystem::path(_path);
auto canonicalPath = boost::filesystem::canonical(path);
bool isAllowed = false;
for (auto const& allowedDir: m_allowedDirectories)
try
{
// If dir is a prefix of boostPath, we are fine.
if (
std::distance(allowedDir.begin(), allowedDir.end()) <= std::distance(canonicalPath.begin(), canonicalPath.end()) &&
std::equal(allowedDir.begin(), allowedDir.end(), canonicalPath.begin())
)
auto path = boost::filesystem::path(_path);
auto canonicalPath = boost::filesystem::canonical(path);
bool isAllowed = false;
for (auto const& allowedDir: m_allowedDirectories)
{
isAllowed = true;
break;
// If dir is a prefix of boostPath, we are fine.
if (
std::distance(allowedDir.begin(), allowedDir.end()) <= std::distance(canonicalPath.begin(), canonicalPath.end()) &&
std::equal(allowedDir.begin(), allowedDir.end(), canonicalPath.begin())
)
{
isAllowed = true;
break;
}
}
if (!isAllowed)
return ReadFile::Result{false, "File outside of allowed directories."};
else if (!boost::filesystem::exists(path))
return ReadFile::Result{false, "File not found."};
else if (!boost::filesystem::is_regular_file(canonicalPath))
return ReadFile::Result{false, "Not a valid file."};
else
{
auto contents = dev::contentsString(canonicalPath.string());
m_sourceCodes[path.string()] = contents;
return ReadFile::Result{true, contents};
}
}
if (!isAllowed)
return ReadFile::Result{false, "File outside of allowed directories."};
else if (!boost::filesystem::exists(path))
return ReadFile::Result{false, "File not found."};
else if (!boost::filesystem::is_regular_file(canonicalPath))
return ReadFile::Result{false, "Not a valid file."};
else
catch (Exception const& _exception)
{
return ReadFile::Result{false, "Exception in read callback: " + boost::diagnostic_information(_exception)};
}
catch (...)
{
auto contents = dev::contentsString(canonicalPath.string());
m_sourceCodes[path.string()] = contents;
return ReadFile::Result{true, contents};
return ReadFile::Result{false, "Unknown exception in read callback."};
}
};

Expand Down

0 comments on commit b7951be

Please sign in to comment.