Skip to content

Commit

Permalink
modify FilePath functions to prevent leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
melissa-barca committed Feb 4, 2020
1 parent f65e568 commit 566a218
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/cpp/shared_core/FilePath.cpp
Expand Up @@ -1117,9 +1117,9 @@ Error FilePath::moveIndirect(const FilePath& in_targetPath) const

Error FilePath::openForRead(std::shared_ptr<std::istream>& out_stream) const
{
std::istream* pResult = nullptr;
try
{
std::istream* pResult = nullptr;
#ifdef _WIN32
using namespace boost::iostreams;
HANDLE hFile = ::CreateFileW(m_impl->Path.wstring().c_str(),
Expand Down Expand Up @@ -1155,6 +1155,8 @@ Error FilePath::openForRead(std::shared_ptr<std::istream>& out_stream) const
}
catch(const std::exception& e)
{
delete pResult;

Error error = systemError(boost::system::errc::io_error,
ERROR_LOCATION);
error.addProperty("what", e.what());
Expand All @@ -1168,9 +1170,9 @@ Error FilePath::openForRead(std::shared_ptr<std::istream>& out_stream) const

Error FilePath::openForWrite(std::shared_ptr<std::ostream>& out_stream, bool in_truncate) const
{
std::ostream* pResult = nullptr;
try
{
std::ostream* pResult = nullptr;
#ifdef _WIN32
using namespace boost::iostreams;
HANDLE hFile = ::CreateFileW(m_impl->Path.wstring().c_str(),
Expand Down Expand Up @@ -1212,6 +1214,7 @@ Error FilePath::openForWrite(std::shared_ptr<std::ostream>& out_stream, bool in_
}
catch(const std::exception& e)
{
delete pResult;
Error error = systemError(boost::system::errc::io_error,
ERROR_LOCATION);
error.addProperty("what", e.what());
Expand Down Expand Up @@ -1291,9 +1294,9 @@ void FilePath::setLastWriteTime(std::time_t in_time) const

Error FilePath::testWritePermissions() const
{
std::ostream* pStream = nullptr;
try
{
std::ostream* pResult = nullptr;
#ifdef _WIN32
using namespace boost::iostreams;
HANDLE hFile = ::CreateFileW(m_impl->Path.wstring().c_str(),
Expand All @@ -1311,16 +1314,16 @@ Error FilePath::testWritePermissions() const
}
file_descriptor_sink fd;
fd.open(hFile, close_handle);
pResult = new boost::iostreams::stream<file_descriptor_sink>(fd);
pStream = new boost::iostreams::stream<file_descriptor_sink>(fd);
#else
using std::ios_base;
ios_base::openmode flags = ios_base::in | ios_base::out | ios_base::binary;
pResult = new std::ofstream(getAbsolutePath().c_str(), flags);
pStream = new std::ofstream(getAbsolutePath().c_str(), flags);
#endif

if (!(*pResult))
if (!(*pStream))
{
delete pResult;
delete pStream;

Error error = systemError(boost::system::errc::no_such_file_or_directory, ERROR_LOCATION);
error.addProperty("path", getAbsolutePath());
Expand All @@ -1329,13 +1332,16 @@ Error FilePath::testWritePermissions() const
}
catch(const std::exception& e)
{
delete pStream;

Error error = systemError(boost::system::errc::io_error,
ERROR_LOCATION);
error.addProperty("what", e.what());
error.addProperty("path", getAbsolutePath());
return error;
}

delete pStream;
return Success();
}

Expand Down

0 comments on commit 566a218

Please sign in to comment.