Skip to content
Closed
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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ PHP NEWS
wrong argument in error messages. (Weilin Du)

- Intl:
. Fixed a memory leak when iterating IntlBreakIterator::getPartsIterator()
results. (iliaal)
. Fixed a double-free when IntlGregorianCalendar construction fails after
the ICU constructor adopts the TimeZone. (iliaal)
. Fixed bug GH-23094 (NumberFormatter parsing offsets use UTF-16 positions
Expand Down
2 changes: 1 addition & 1 deletion ext/intl/breakiterator/breakiterator_iterators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void IntlIterator_from_BreakIterator_parts(zval *break_iter_zv,
ii->iterator->index = 0;

((zoi_with_current*)ii->iterator)->destroy_it = _breakiterator_parts_destroy_it;
ZVAL_OBJ_COPY(&((zoi_with_current*)ii->iterator)->wrapping_obj, Z_OBJ_P(object));
ZVAL_UNDEF(&((zoi_with_current*)ii->iterator)->wrapping_obj);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the teardown in zoi_with_current_dtor() needs to move into the iterator's own dtor, otherwise _breakiterator_parts_destroy_it() releases the BreakIterator mid iteration and zoi_bit->bio dangles. Iterating a temporary parts iterator segfaults on the second element today, and a test doing that (iterating a call result directly, not a variable) should come with it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved destroy_it into the iterator dtor. Test iterates a call result.

ZVAL_UNDEF(&((zoi_with_current*)ii->iterator)->current);

((zoi_break_iter_parts*)ii->iterator)->bio = Z_INTL_BREAKITERATOR_P(break_iter_zv);
Expand Down
3 changes: 2 additions & 1 deletion ext/intl/common/common_enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ zend_object_handlers IntlIterator_handlers;
void zoi_with_current_dtor(zend_object_iterator *iter)
{
zoi_with_current *zoiwc = (zoi_with_current*)iter;
iter->funcs->invalidate_current(iter);
zoiwc->destroy_it(iter);
zval_ptr_dtor(&zoiwc->wrapping_obj);
ZVAL_UNDEF(&zoiwc->wrapping_obj);
}
Expand Down Expand Up @@ -147,7 +149,6 @@ static void IntlIterator_objects_dtor(zend_object *object)
{
IntlIterator_object *ii = php_intl_iterator_fetch_object(object);
if (ii->iterator) {
((zoi_with_current*)ii->iterator)->destroy_it(ii->iterator);
OBJ_RELEASE(&ii->iterator->std);
ii->iterator = NULL;
}
Expand Down
31 changes: 31 additions & 0 deletions ext/intl/tests/breakiter_parts_iterator_current_leak.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
IntlPartsIterator must not leak, and a temporary one must not dangle
--EXTENSIONS--
intl
--FILE--
<?php
function parts(): IntlPartsIterator {
$bi = IntlBreakIterator::createWordInstance('en');
$bi->setText('hello world');
return $bi->getPartsIterator();
}

foreach (parts() as $part) {
echo "[$part]\n";
}

$bi = IntlBreakIterator::createWordInstance('en');
$bi->setText('hello world foo bar baz');
$m0 = memory_get_usage();
for ($i = 0; $i < 20000; $i++) {
foreach ($bi->getPartsIterator() as $v) {
break;
}
}
var_dump(memory_get_usage() - $m0 < 1024 * 1024);
?>
--EXPECT--
[hello]
[ ]
[world]
bool(true)
Loading