Prefer ZVAL_COPY

Instead of ZVAL_COPY_VALUE + Z_TRY_ADDREF. Also fix another leak
in SplDoublyLinkedList::add(), the push case was leaking as well.
This commit is contained in:
Nikita Popov 2021-04-19 14:35:36 +02:00
parent 497fadcacd
commit 044de83635
2 changed files with 6 additions and 9 deletions

View file

@ -168,7 +168,7 @@ static void spl_ptr_llist_unshift(spl_ptr_llist *llist, zval *data) /* {{{ */
elem->prev = NULL; elem->prev = NULL;
elem->next = llist->head; elem->next = llist->head;
ZVAL_COPY_VALUE(&elem->data, data); ZVAL_COPY(&elem->data, data);
SPL_LLIST_RC(elem) = 1; SPL_LLIST_RC(elem) = 1;
if (llist->head) { if (llist->head) {
@ -179,8 +179,6 @@ static void spl_ptr_llist_unshift(spl_ptr_llist *llist, zval *data) /* {{{ */
llist->head = elem; llist->head = elem;
llist->count++; llist->count++;
Z_TRY_ADDREF(elem->data);
} }
/* }}} */ /* }}} */
@ -190,7 +188,7 @@ static void spl_ptr_llist_push(spl_ptr_llist *llist, zval *data) /* {{{ */
elem->prev = llist->tail; elem->prev = llist->tail;
elem->next = NULL; elem->next = NULL;
ZVAL_COPY_VALUE(&elem->data, data); ZVAL_COPY(&elem->data, data);
SPL_LLIST_RC(elem) = 1; SPL_LLIST_RC(elem) = 1;
if (llist->tail) { if (llist->tail) {
@ -201,8 +199,6 @@ static void spl_ptr_llist_push(spl_ptr_llist *llist, zval *data) /* {{{ */
llist->tail = elem; llist->tail = elem;
llist->count++; llist->count++;
Z_TRY_ADDREF(elem->data);
} }
/* }}} */ /* }}} */
@ -1189,7 +1185,6 @@ PHP_METHOD(SplDoublyLinkedList, add)
RETURN_THROWS(); RETURN_THROWS();
} }
Z_TRY_ADDREF_P(value);
if (index == intern->llist->count) { if (index == intern->llist->count) {
/* If index is the last entry+1 then we do a push because we're not inserting before any entry */ /* If index is the last entry+1 then we do a push because we're not inserting before any entry */
spl_ptr_llist_push(intern->llist, value); spl_ptr_llist_push(intern->llist, value);
@ -1200,7 +1195,7 @@ PHP_METHOD(SplDoublyLinkedList, add)
/* Get the element we want to insert before */ /* Get the element we want to insert before */
element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO); element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
ZVAL_COPY_VALUE(&elem->data, value); ZVAL_COPY(&elem->data, value);
SPL_LLIST_RC(elem) = 1; SPL_LLIST_RC(elem) = 1;
/* connect to the neighbours */ /* connect to the neighbours */
elem->next = element; elem->next = element;

View file

@ -33,8 +33,9 @@ echo $dll->pop()."\n";
// Test refcounted value // Test refcounted value
$str = "foo"; $str = "foo";
$str .= "bar"; $str .= "bar";
$dll->add(0, null);
$dll->add(0, $str); $dll->add(0, $str);
$dll->add(0, $str);
var_dump($dll->shift());
var_dump($dll->shift()); var_dump($dll->shift());
?> ?>
@ -49,3 +50,4 @@ Exception: SplDoublyLinkedList::add(): Argument #1 ($index) is out of range
2 2
1 1
string(6) "foobar" string(6) "foobar"
string(6) "foobar"