Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 644de0d

Browse files
authored
Merge pull request #6973 from runrevmark/bugfix-21997
[[ Bug 21997 ]] Fix memory leak in MCStringSplitByDelimiter
2 parents 0bf3ff6 + 1736c9f commit 644de0d

File tree

2 files changed

+25
-46
lines changed

2 files changed

+25
-46
lines changed

docs/notes/bugfix-21997.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Fix memory leaks when splitting strings in specific cases
2+

libfoundation/src/foundation-string.cpp

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5571,8 +5571,6 @@ bool MCStringSplitByDelimiter(MCStringRef self, MCStringRef p_elem_del, MCString
55715571
return MCStringSplitByDelimiterNative(self, p_elem_del, p_options, r_list);
55725572
}
55735573

5574-
MCAutoArray<MCValueRef> t_strings;
5575-
55765574
if (__MCStringIsIndirect(p_elem_del))
55775575
p_elem_del = p_elem_del -> string;
55785576

@@ -5599,44 +5597,33 @@ bool MCStringSplitByDelimiter(MCStringRef self, MCStringRef p_elem_del, MCString
55995597
bool t_success;
56005598
t_success = true;
56015599

5600+
MCAutoStringRefArray t_strings;
56025601
for(;;)
56035602
{
56045603
uindex_t t_found_del_length, t_end_offset;
56055604

56065605
split_find_end_of_element(t_sptr, t_to_end, self_native, t_echar, t_del_length, del_native, p_options, t_end_offset, t_found_del_length);
56075606

5608-
MCStringRef t_string;
5609-
t_string = nil;
5610-
5611-
if (t_success)
5612-
t_success = MCStringCopySubstring(self, MCRangeMake(t_offset, t_end_offset), t_string);
5613-
5614-
if (t_success)
5615-
t_success = t_strings . Push(t_string);
5616-
5617-
if (!t_success)
5618-
break;
5607+
MCAutoStringRef t_string;
5608+
if (!MCStringCopySubstring(self, MCRangeMake(t_offset, t_end_offset), &t_string))
5609+
{
5610+
return false;
5611+
}
56195612

5613+
if (!t_strings.Push(*t_string))
5614+
{
5615+
return false;
5616+
}
5617+
56205618
if (t_end_offset + t_found_del_length >= t_to_end)
56215619
break;
56225620

56235621
t_offset += t_end_offset + t_found_del_length;
56245622
t_sptr = (const char *)t_sptr + (self_native ? t_end_offset + t_found_del_length : 2 * (t_end_offset + t_found_del_length));
56255623
t_to_end -= (t_end_offset + t_found_del_length);
56265624
}
5627-
5628-
if (t_success)
5629-
t_success = MCProperListCreate(t_strings . Ptr(), t_strings .Size(), r_list);
5630-
5631-
if (!t_success)
5632-
{
5633-
for (uindex_t i = 0; i < t_strings . Size(); i++)
5634-
MCValueRelease(t_strings[i]);
5635-
5636-
return false;
5637-
}
56385625

5639-
return true;
5626+
return t_strings.TakeAsProperList(r_list);
56405627
}
56415628

56425629
static bool
@@ -5648,8 +5635,6 @@ MCStringSplitByDelimiterNative(MCStringRef self, MCStringRef p_elem_del, MCStrin
56485635
if (__MCStringIsIndirect(p_elem_del))
56495636
p_elem_del = p_elem_del -> string;
56505637

5651-
MCAutoArray<MCValueRef> t_strings;
5652-
56535638
const char_t *t_sptr;
56545639
const char_t *t_eptr;
56555640
t_sptr = self -> native_chars;
@@ -5658,19 +5643,22 @@ MCStringSplitByDelimiterNative(MCStringRef self, MCStringRef p_elem_del, MCStrin
56585643
bool t_success;
56595644
t_success = true;
56605645

5646+
MCAutoStringRefArray t_strings;
56615647
for(;;)
56625648
{
56635649
const char_t *t_element_end = NULL;
56645650
split_find_end_of_element_native(t_sptr, t_eptr, p_elem_del -> native_chars, p_elem_del -> char_count, t_element_end, p_options);
56655651

5666-
MCStringRef t_string;
5667-
t_string = nil;
5668-
5669-
if (t_success)
5670-
t_success = MCStringCreateWithNativeChars(t_sptr, t_element_end - t_sptr, t_string);
5652+
MCAutoStringRef t_string;
5653+
if (!MCStringCreateWithNativeChars(t_sptr, t_element_end - t_sptr, &t_string))
5654+
{
5655+
return false;
5656+
}
56715657

5672-
if (t_success)
5673-
t_success = t_strings . Push(t_string);
5658+
if (!t_strings.Push(*t_string))
5659+
{
5660+
return false;
5661+
}
56745662

56755663
if (t_element_end + p_elem_del -> char_count >= t_eptr)
56765664
break;
@@ -5681,18 +5669,7 @@ MCStringSplitByDelimiterNative(MCStringRef self, MCStringRef p_elem_del, MCStrin
56815669
t_sptr = t_element_end + p_elem_del -> char_count;
56825670
}
56835671

5684-
if (t_success)
5685-
t_success = MCProperListCreate(t_strings . Ptr(), t_strings .Size(), r_list);
5686-
5687-
if (!t_success)
5688-
{
5689-
for (uindex_t i = 0; i < t_strings . Size(); i++)
5690-
MCValueRelease(t_strings[i]);
5691-
5692-
return false;
5693-
}
5694-
5695-
return true;
5672+
return t_strings.TakeAsProperList(r_list);
56965673
}
56975674

56985675
MC_DLLEXPORT_DEF

0 commit comments

Comments
 (0)