Skip to content
Closed
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
89 changes: 58 additions & 31 deletions solc/CommandLineInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,35 @@ void CommandLineInterface::handleFormal()
cout << "Formal version:" << endl << m_compiler->formalTranslation() << endl;
}

void CommandLineInterface::readInputFilesAndConfigureRemappings()
void CommandLineInterface::readInputFilesAndConfigureRemappings(bool usestdin, vector<string> inputFiles)
Copy link
Contributor

Choose a reason for hiding this comment

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

Arguments should have _ as prefix and vectors should be passed by const reference.

{
if (!m_args.count("input-file"))
for (string path: inputFiles)
{
auto eq = find(path.begin(), path.end(), '=');
if (eq != path.end())
path = string(eq + 1, path.end());
else
{
auto infile = boost::filesystem::path(path);
if (!boost::filesystem::exists(infile))
{
cerr << "Skipping non existant input file \"" << infile << "\"" << endl;
continue;
}

if (!boost::filesystem::is_regular_file(infile))
{
cerr << "\"" << infile << "\" is not a valid file. Skipping" << endl;
continue;
}

m_sourceCodes[infile.string()] = dev::contentsString(infile.string());
path = boost::filesystem::canonical(infile).string();
}
m_allowedDirectories.push_back(boost::filesystem::path(path).remove_filename());
}

if (usestdin)
{
string s;
while (!cin.eof())
Expand All @@ -317,32 +343,6 @@ void CommandLineInterface::readInputFilesAndConfigureRemappings()
m_sourceCodes[g_stdinFileName].append(s + '\n');
}
}
else
for (string path: m_args["input-file"].as<vector<string>>())
{
auto eq = find(path.begin(), path.end(), '=');
if (eq != path.end())
path = string(eq + 1, path.end());
else
{
auto infile = boost::filesystem::path(path);
if (!boost::filesystem::exists(infile))
{
cerr << "Skipping non existant input file \"" << infile << "\"" << endl;
continue;
}

if (!boost::filesystem::is_regular_file(infile))
{
cerr << "\"" << infile << "\" is not a valid file. Skipping" << endl;
continue;
}

m_sourceCodes[infile.string()] = dev::contentsString(infile.string());
path = boost::filesystem::canonical(infile).string();
}
m_allowedDirectories.push_back(boost::filesystem::path(path).remove_filename());
}
}

bool CommandLineInterface::parseLibraryOption(string const& _input)
Expand Down Expand Up @@ -402,6 +402,8 @@ outputs the components specified in the options at standard output or in files i
the output directory, if specified.
Example: solc --bin -o /tmp/solcoutput contract.sol

When - is specified as one of the input files it will add the current directory to the allowed directories and read from standard input
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the justification for adding the current directory to the allowed directories if you use stdin?
I think there is a good argument to actually not do it. Note that the "allowed directories" is a security feature, not a way to find input files.


Allowed options)",
po::options_description::m_default_line_length,
po::options_description::m_default_line_length - 23);
Expand Down Expand Up @@ -509,7 +511,32 @@ Allowed options)",

bool CommandLineInterface::processInput()
{
readInputFilesAndConfigureRemappings();
// determine if we should use stdin
bool usestdin = false;
vector<string> inputFiles;

if (!m_args.count("input-file")) {
inputFiles = vector<string>();
usestdin = true;
} else {
inputFiles = m_args["input-file"].as<vector<string>>();

if (inputFiles.size() == 0)
usestdin = true;
else
// search for "-" in input files to enable stdin (and filter out the "-")
for (unsigned idx = 0; idx < inputFiles.size(); ++idx) {
string path = inputFiles[idx];
if (path == "-") {
inputFiles.erase(inputFiles.begin() + idx);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be better to loop through m_args["input-fil"].as<vector<string>>() and add everything to inputFiles that is not "-".

Also the current routine does not work correctly if you specify - twice.

usestdin = true;
m_allowedDirectories.push_back(boost::filesystem::current_path());
break;
}
}
}

readInputFilesAndConfigureRemappings(usestdin, inputFiles);

if (m_args.count("libraries"))
for (string const& library: m_args["libraries"].as<vector<string>>())
Expand Down Expand Up @@ -564,8 +591,8 @@ bool CommandLineInterface::processInput()
auto scannerFromSourceName = [&](string const& _sourceName) -> solidity::Scanner const& { return m_compiler->scanner(_sourceName); };
try
{
if (m_args.count("input-file"))
m_compiler->setRemappings(m_args["input-file"].as<vector<string>>());
if (inputFiles.size())
m_compiler->setRemappings(inputFiles);
for (auto const& sourceCode: m_sourceCodes)
m_compiler->addSource(sourceCode.first, sourceCode.second);
// TODO: Perhaps we should not compile unless requested
Expand Down
3 changes: 2 additions & 1 deletion solc/CommandLineInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/inlineasm/AsmStack.h>

using namespace std;
namespace dev
{
namespace solidity
Expand Down Expand Up @@ -68,7 +69,7 @@ class CommandLineInterface
void handleFormal();

/// Fills @a m_sourceCodes initially and @a m_redirects.
void readInputFilesAndConfigureRemappings();
void readInputFilesAndConfigureRemappings(bool usestdin, vector<string> inputFiles);
/// Tries to read from the file @a _input or interprets _input literally if that fails.
/// It then tries to parse the contents and appends to m_libraries.
bool parseLibraryOption(std::string const& _input);
Expand Down