Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix potential memory leak. #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/entry_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,16 @@ entry_iterator::entry_iterator(const Path* p_directory)
mp_cur(NULL),
mp_cur_path(new Path())
{
open_native_handle();
try
{
open_native_handle();
}
catch (...)
{
delete mp_cur_path;
mp_cur_path = NULL;
throw;
}
}

/**
Expand Down Expand Up @@ -267,12 +276,12 @@ entry_iterator& entry_iterator::operator=(const entry_iterator& other)
{
mp_directory = other.mp_directory;
mp_cur = other.mp_cur;
mp_cur_path = other.mp_cur_path;
*mp_cur_path = *other.mp_cur_path;

entry_iterator& e = const_cast<entry_iterator&>(other);
e.mp_directory = NULL;
e.mp_cur = NULL;
e.mp_cur_path = new Path();
*e.mp_cur_path = Path();

return *this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ WindowsError::WindowsError(DWORD val)
ss << val;

wchar_t* buf = NULL;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL,
val,
LANG_USER_DEFAULT,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(wchar_t*) &buf, // What a weird API.
0,
NULL);
Expand Down
2 changes: 1 addition & 1 deletion src/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Path::localpathtype Path::c_localdefault = LOCALPATH_LOCAL;
*/
Path::Path()
{
m_path = ".";
m_path = "";
}

/**
Expand Down
17 changes: 13 additions & 4 deletions src/pathie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ std::string Pathie::utf16_to_utf8(std::wstring str)
size = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.length(), utf8, size, NULL, NULL);

if (size == 0)
throw(Pathie::WindowsError(GetLastError()));
{
free(utf8);
throw(Pathie::WindowsError(GetLastError()));
}

std::string utf8str(utf8, size);
free(utf8);
Expand All @@ -69,7 +72,10 @@ std::wstring Pathie::utf8_to_utf16(std::string str)
count = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), utf16, count);

if (count == 0)
throw(Pathie::WindowsError(GetLastError()));
{
free(utf16);
throw(Pathie::WindowsError(GetLastError()));
}

std::wstring utf16str(utf16, count);
free(utf16);
Expand Down Expand Up @@ -122,8 +128,11 @@ std::string Pathie::convert_encodings(const char* from_encoding, const char* to_
size_t outbytes_left = 0;
size_t inbytes_left = input_length;

if (converter == (iconv_t) -1)
throw Pathie::ErrnoError(errno);
if (converter == (iconv_t)-1)
{
free(copy);
throw Pathie::ErrnoError(errno);
}

/* There is no way to know how much space iconv() will need. So we keep
* allocating more and more memory as needed. `current_size' keeps track
Expand Down