From 3c659c2737b26d8ee28c727b277325852df8dd09 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 21 Jan 2015 01:24:37 +0100 Subject: Do only perform relative file lookups if a relative path is given (to allow users to include files without accidently including a global resource) --- src/plugins/filesystem/FileLocator.cpp | 5 +++++ test/plugins/filesystem/FileLocatorTest.cpp | 20 ++++++++++++++++++++ testdata/filesystem/b/d.txt | 1 + 3 files changed, 26 insertions(+) create mode 100644 testdata/filesystem/b/d.txt diff --git a/src/plugins/filesystem/FileLocator.cpp b/src/plugins/filesystem/FileLocator.cpp index 467363b..af5244c 100644 --- a/src/plugins/filesystem/FileLocator.cpp +++ b/src/plugins/filesystem/FileLocator.cpp @@ -143,6 +143,11 @@ bool FileLocator::doLocate(Resource &resource, const std::string &path, } } + // If the path starts with "./" only perform relative lookups! + if (path.substr(0, 2) == "./") { + return false; + } + // Otherwise look in the search paths, search backwards, last defined search // paths have a higher precedence auto it = searchPaths.find(type); diff --git a/test/plugins/filesystem/FileLocatorTest.cpp b/test/plugins/filesystem/FileLocatorTest.cpp index 17d43dd..beb091d 100644 --- a/test/plugins/filesystem/FileLocatorTest.cpp +++ b/test/plugins/filesystem/FileLocatorTest.cpp @@ -142,6 +142,26 @@ TEST(FileLocator, testLocate) assert_not_located(locator, "c.txt", "", ResourceType::SCRIPT); } +TEST(FileLocator, testLocateRelative) +{ + FileLocator locator; + locator.addUnittestSearchPath("filesystem"); + + // Add the respective search path + locator.addUnittestSearchPath("filesystem/b"); + + Resource resA, resC; + ASSERT_TRUE(locator.locate(resA, "a.txt")); + ASSERT_TRUE(locator.locate(resC, "c.txt")); + + Resource resD; + ASSERT_TRUE(locator.locate(resD, "d.txt")); + ASSERT_TRUE(locator.locate(resD, "d.txt", ResourceType::UNKNOWN, resA)); + ASSERT_TRUE(locator.locate(resD, "d.txt", ResourceType::UNKNOWN, resC)); + ASSERT_FALSE(locator.locate(resD, "./d.txt", ResourceType::UNKNOWN, resA)); + ASSERT_TRUE(locator.locate(resD, "./d.txt", ResourceType::UNKNOWN, resC)); +} + TEST(FileLocator, testStream) { FileLocator locator; diff --git a/testdata/filesystem/b/d.txt b/testdata/filesystem/b/d.txt new file mode 100644 index 0000000..3e676fe --- /dev/null +++ b/testdata/filesystem/b/d.txt @@ -0,0 +1 @@ +file d -- cgit v1.2.3