WFS: Normalize paths before opening.

This commit is contained in:
Pierre Bourdon committed 2017-08-15 22:35:45 +02:00
1 parent 425cf18bf7
commit 2f5ddf12a9
2 files changed
+22 -4

No files matched your search

+21 -3
View File
@@ -135,7 +135,7 @@ IPCCommandResult WFSSRV::IOCtl(const IOCtlRequest& request)
u16 path_len = Memory::Read_U16(request.buffer_in + 0x20); u16 path_len = Memory::Read_U16(request.buffer_in + 0x20);
std::string path = Memory::GetString(request.buffer_in + 0x22, path_len); std::string path = Memory::GetString(request.buffer_in + 0x22, path_len);
path = ExpandPath(path); path = NormalizePath(path);
u16 fd = GetNewFileDescriptor(); u16 fd = GetNewFileDescriptor();
FileDescriptor* fd_obj = &m_fds[fd]; FileDescriptor* fd_obj = &m_fds[fd];
@@ -204,7 +204,7 @@ IPCCommandResult WFSSRV::IOCtl(const IOCtlRequest& request)
return GetDefaultReply(return_error_code); 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; std::string expanded;
if (!path.empty() && path[0] == '~') if (!path.empty() && path[0] == '~')
@@ -219,7 +219,25 @@ std::string WFSSRV::ExpandPath(const std::string& path) const
{ {
expanded = path; expanded = path;
} }
return expanded;
std::vector<std::string> components = SplitString(expanded, '/');
std::vector<std::string> 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) WFSSRV::FileDescriptor* WFSSRV::FindFileDescriptor(u16 fd)
+1 -1
View File
@@ -39,7 +39,7 @@ private:
std::string m_home_directory; std::string m_home_directory;
std::string m_current_directory; std::string m_current_directory;
std::string ExpandPath(const std::string& path) const; std::string NormalizePath(const std::string& path) const;
enum enum
{ {