diff --git a/libsolidity/interface/FileReader.cpp b/libsolidity/interface/FileReader.cpp index b7ff2ce58..06a0381cf 100644 --- a/libsolidity/interface/FileReader.cpp +++ b/libsolidity/interface/FileReader.cpp @@ -33,11 +33,29 @@ using std::string; namespace solidity::frontend { +FileReader::FileReader( + boost::filesystem::path _basePath, + FileSystemPathSet _allowedDirectories +): + m_allowedDirectories(std::move(_allowedDirectories)), + m_sourceCodes() +{ + setBasePath(_basePath); + for (boost::filesystem::path const& allowedDir: m_allowedDirectories) + solAssert(!allowedDir.empty(), ""); +} + void FileReader::setBasePath(boost::filesystem::path const& _path) { m_basePath = (_path.empty() ? "" : normalizeCLIPathForVFS(_path)); } +void FileReader::allowDirectory(boost::filesystem::path _path) +{ + solAssert(!_path.empty(), ""); + m_allowedDirectories.insert(std::move(_path)); +} + void FileReader::setSource(boost::filesystem::path const& _path, SourceCode _source) { boost::filesystem::path normalizedPath = normalizeCLIPathForVFS(_path); diff --git a/libsolidity/interface/FileReader.h b/libsolidity/interface/FileReader.h index 53d4ef9b8..fe59464ad 100644 --- a/libsolidity/interface/FileReader.h +++ b/libsolidity/interface/FileReader.h @@ -46,20 +46,12 @@ public: /// Constructs a FileReader with a base path and a set of allowed directories that /// will be used when requesting files from this file reader instance. - explicit FileReader( - boost::filesystem::path _basePath = {}, - FileSystemPathSet _allowedDirectories = {} - ): - m_allowedDirectories(std::move(_allowedDirectories)), - m_sourceCodes() - { - setBasePath(_basePath); - } + explicit FileReader(boost::filesystem::path _basePath = {}, FileSystemPathSet _allowedDirectories = {}); void setBasePath(boost::filesystem::path const& _path); boost::filesystem::path const& basePath() const noexcept { return m_basePath; } - void allowDirectory(boost::filesystem::path _path) { m_allowedDirectories.insert(std::move(_path)); } + void allowDirectory(boost::filesystem::path _path); FileSystemPathSet const& allowedDirectories() const noexcept { return m_allowedDirectories; } StringMap const& sourceCodes() const noexcept { return m_sourceCodes; } diff --git a/solc/CommandLineParser.cpp b/solc/CommandLineParser.cpp index 64fc5e630..af67c7360 100644 --- a/solc/CommandLineParser.cpp +++ b/solc/CommandLineParser.cpp @@ -358,7 +358,7 @@ bool CommandLineParser::parseInputPathsAndRemappings() boost::filesystem::path remappingDir = remapping->target; remappingDir.remove_filename(); - m_options.input.allowedDirectories.insert(remappingDir); + m_options.input.allowedDirectories.insert(remappingDir.empty() ? "." : remappingDir); m_options.input.remappings.emplace_back(move(remapping.value())); } @@ -979,7 +979,8 @@ bool CommandLineParser::processArgs() { vector paths; for (string const& allowedPath: boost::split(paths, m_args[g_strAllowPaths].as(), boost::is_any_of(","))) - m_options.input.allowedDirectories.insert(allowedPath); + if (!allowedPath.empty()) + m_options.input.allowedDirectories.insert(allowedPath); } if (m_args.count(g_strStopAfter)) diff --git a/test/solc/CommandLineInterfaceAllowPaths.cpp b/test/solc/CommandLineInterfaceAllowPaths.cpp index ae6e1912b..f09873d7b 100644 --- a/test/solc/CommandLineInterfaceAllowPaths.cpp +++ b/test/solc/CommandLineInterfaceAllowPaths.cpp @@ -288,8 +288,8 @@ BOOST_FIXTURE_TEST_CASE(allow_path_should_handle_empty_paths, AllowPathsFixture) BOOST_TEST(checkImport("import 'a/../../code/a/b/c.sol'", {"--allow-paths", "x,,y"}) == ImportCheck::PathDisallowed()); // Work dir is not base path - BOOST_TEST(checkImport("import 'a/../../work/a/b/c.sol'", {"--allow-paths", "", "--base-path=../code/"})); - BOOST_TEST(checkImport("import 'a/../../work/a/b/c.sol'", {"--allow-paths", "x,,y", "--base-path=../code/"})); + BOOST_TEST(checkImport("import 'a/../../work/a/b/c.sol'", {"--allow-paths", "", "--base-path=../code/"}) == ImportCheck::PathDisallowed()); + BOOST_TEST(checkImport("import 'a/../../work/a/b/c.sol'", {"--allow-paths", "x,,y", "--base-path=../code/"}) == ImportCheck::PathDisallowed()); BOOST_TEST(checkImport("import 'a/../../code/a/b/c.sol'", {"--allow-paths", "", "--base-path=../code/"})); BOOST_TEST(checkImport("import 'a/../../code/a/b/c.sol'", {"--allow-paths", "x,,y", "--base-path=../code/"})); } diff --git a/test/solc/CommandLineParser.cpp b/test/solc/CommandLineParser.cpp index 26c620f03..142ba31af 100644 --- a/test/solc/CommandLineParser.cpp +++ b/test/solc/CommandLineParser.cpp @@ -168,7 +168,7 @@ BOOST_AUTO_TEST_CASE(cli_mode_options) expectedOptions.input.addStdin = true; expectedOptions.input.basePath = "/home/user/"; - expectedOptions.input.allowedDirectories = {"/tmp", "/home", "project", "../contracts", "", "c", "/usr/lib"}; + expectedOptions.input.allowedDirectories = {"/tmp", "/home", "project", "../contracts", ".", "c", "/usr/lib"}; expectedOptions.input.ignoreMissingFiles = true; expectedOptions.input.errorRecovery = (inputMode == InputMode::Compiler); expectedOptions.output.dir = "/tmp/out"; @@ -307,7 +307,7 @@ BOOST_AUTO_TEST_CASE(assembly_mode_options) }; expectedOptions.input.addStdin = true; expectedOptions.input.basePath = "/home/user/"; - expectedOptions.input.allowedDirectories = {"/tmp", "/home", "project", "../contracts", "", "c", "/usr/lib"}; + expectedOptions.input.allowedDirectories = {"/tmp", "/home", "project", "../contracts", ".", "c", "/usr/lib"}; expectedOptions.input.ignoreMissingFiles = true; expectedOptions.output.overwriteFiles = true; expectedOptions.output.evmVersion = EVMVersion::spuriousDragon();