Skip to content

Commit 1ddbb83

Browse files
committed
fix potential memory leak.
1 parent 7e3766b commit 1ddbb83

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/entry_iterator.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,16 @@ entry_iterator::entry_iterator(const Path* p_directory)
6464
mp_cur(NULL),
6565
mp_cur_path(new Path())
6666
{
67-
open_native_handle();
67+
try
68+
{
69+
open_native_handle();
70+
}
71+
catch (...)
72+
{
73+
delete mp_cur_path;
74+
mp_cur_path = NULL;
75+
throw;
76+
}
6877
}
6978

7079
/**
@@ -267,12 +276,12 @@ entry_iterator& entry_iterator::operator=(const entry_iterator& other)
267276
{
268277
mp_directory = other.mp_directory;
269278
mp_cur = other.mp_cur;
270-
mp_cur_path = other.mp_cur_path;
279+
*mp_cur_path = *other.mp_cur_path;
271280

272281
entry_iterator& e = const_cast<entry_iterator&>(other);
273282
e.mp_directory = NULL;
274283
e.mp_cur = NULL;
275-
e.mp_cur_path = new Path();
284+
*e.mp_cur_path = Path();
276285

277286
return *this;
278287
}

src/pathie.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ std::string Pathie::utf16_to_utf8(std::wstring str)
4747
size = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.length(), utf8, size, NULL, NULL);
4848

4949
if (size == 0)
50-
throw(Pathie::WindowsError(GetLastError()));
50+
{
51+
free(utf8);
52+
throw(Pathie::WindowsError(GetLastError()));
53+
}
5154

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

7174
if (count == 0)
72-
throw(Pathie::WindowsError(GetLastError()));
75+
{
76+
free(utf16);
77+
throw(Pathie::WindowsError(GetLastError()));
78+
}
7379

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

125-
if (converter == (iconv_t) -1)
126-
throw Pathie::ErrnoError(errno);
131+
if (converter == (iconv_t)-1)
132+
{
133+
free(copy);
134+
throw Pathie::ErrnoError(errno);
135+
}
127136

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

0 commit comments

Comments
 (0)