diff --git a/lib/filelister.cpp b/lib/filelister.cpp index 265879c94..38138e9ab 100644 --- a/lib/filelister.cpp +++ b/lib/filelister.cpp @@ -59,8 +59,9 @@ std::string FileLister::simplifyPath(const char *originalPath) for (std::vector::size_type i = 0; i < pathParts.size(); ++i) { - if (pathParts[i] == ".." && i > 1) + if (pathParts[i] == ".." && i > 1 && pathParts.size() > i + 1) { + pathParts.erase(pathParts.begin() + i + 1); pathParts.erase(pathParts.begin() + i); pathParts.erase(pathParts.begin() + i - 1); pathParts.erase(pathParts.begin() + i - 2); diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index fba95c7e8..12f9d243c 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1262,28 +1262,16 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename if (headerType == 0) continue; - std::string tempFile = filename; - std::transform(tempFile.begin(), tempFile.end(), tempFile.begin(), tolowerWrapper); - if (handledFiles.find(tempFile) != handledFiles.end()) - { - // We have processed this file already once, skip - // it this time to avoid ethernal loop. - continue; - } - - handledFiles.insert(tempFile); - // filename contains now a file name e.g. "menu.h" std::string processedFile; bool fileOpened = false; + std::ifstream fin; for (std::list::const_iterator iter = includePaths.begin(); iter != includePaths.end(); ++iter) { - std::ifstream fin; fin.open((*iter + filename).c_str()); if (fin.is_open()) { filename = *iter + filename; - processedFile = Preprocessor::read(fin, filename, _settings); fileOpened = true; break; } @@ -1292,14 +1280,30 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename if (headerType == 1 && !fileOpened) { filename = paths.back() + filename; - std::ifstream fin(filename.c_str()); + fin.open(filename.c_str()); if (fin.is_open()) { - processedFile = Preprocessor::read(fin, filename, _settings); fileOpened = true; } } + if (fileOpened) + { + std::string tempFile = FileLister::simplifyPath(filename.c_str()); + std::transform(tempFile.begin(), tempFile.end(), tempFile.begin(), tolowerWrapper); + if (handledFiles.find(tempFile) != handledFiles.end()) + { + // We have processed this file already once, skip + // it this time to avoid ethernal loop. + continue; + } + + handledFiles.insert(tempFile); + std::ifstream fin(filename.c_str()); + processedFile = Preprocessor::read(fin, filename, _settings); + fin.close(); + } + if (processedFile.length() > 0) { // Replace all tabs with spaces.. diff --git a/test/testfilelister.cpp b/test/testfilelister.cpp index 561460a05..2886e467c 100644 --- a/test/testfilelister.cpp +++ b/test/testfilelister.cpp @@ -45,6 +45,8 @@ private: ASSERT_EQUALS("/index.h", FileLister::simplifyPath("/path/../other/../index.h")); ASSERT_EQUALS("/index.h", FileLister::simplifyPath("/path/../other///././../index.h")); ASSERT_EQUALS("../path/index.h", FileLister::simplifyPath("../path/other/../index.h")); + ASSERT_EQUALS("a/index.h", FileLister::simplifyPath("a/../a/index.h")); + ASSERT_EQUALS("a/..", FileLister::simplifyPath("a/..")); }