From 2f5ddf12a9058cef80f17a1fc3635fc2b3f46c18 Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Sun, 6 Aug 2017 22:18:20 +0200 Subject: [PATCH] WFS: Normalize paths before opening. --- Source/Core/Core/IOS/WFS/WFSSRV.cpp | 24 +++++++++++++++++++++--- Source/Core/Core/IOS/WFS/WFSSRV.h | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Source/Core/Core/IOS/WFS/WFSSRV.cpp b/Source/Core/Core/IOS/WFS/WFSSRV.cpp index 1f82768ad4..6ff55a8e66 100644 --- a/Source/Core/Core/IOS/WFS/WFSSRV.cpp +++ b/Source/Core/Core/IOS/WFS/WFSSRV.cpp @@ -135,7 +135,7 @@ IPCCommandResult WFSSRV::IOCtl(const IOCtlRequest& request) u16 path_len = Memory::Read_U16(request.buffer_in + 0x20); std::string path = Memory::GetString(request.buffer_in + 0x22, path_len); - path = ExpandPath(path); + path = NormalizePath(path); u16 fd = GetNewFileDescriptor(); FileDescriptor* fd_obj = &m_fds[fd]; @@ -204,7 +204,7 @@ IPCCommandResult WFSSRV::IOCtl(const IOCtlRequest& request) return GetDefaultReply(return_error_code); } -std::string WFSSRV::ExpandPath(const std::string& path) const +std::string WFSSRV::NormalizePath(const std::string& path) const { std::string expanded; if (!path.empty() && path[0] == '~') @@ -219,7 +219,25 @@ std::string WFSSRV::ExpandPath(const std::string& path) const { expanded = path; } - return expanded; + + std::vector components = SplitString(expanded, '/'); + std::vector normalized_components; + for (const auto& component : components) + { + if (component.empty() || component == ".") + { + continue; + } + else if (component == ".." && !normalized_components.empty()) + { + normalized_components.pop_back(); + } + else + { + normalized_components.push_back(component); + } + } + return "/" + JoinStrings(normalized_components, "/"); } WFSSRV::FileDescriptor* WFSSRV::FindFileDescriptor(u16 fd) diff --git a/Source/Core/Core/IOS/WFS/WFSSRV.h b/Source/Core/Core/IOS/WFS/WFSSRV.h index 86302a80bb..889bfc25fd 100644 --- a/Source/Core/Core/IOS/WFS/WFSSRV.h +++ b/Source/Core/Core/IOS/WFS/WFSSRV.h @@ -39,7 +39,7 @@ private: std::string m_home_directory; std::string m_current_directory; - std::string ExpandPath(const std::string& path) const; + std::string NormalizePath(const std::string& path) const; enum {