From 044de83635400656dcbb6da530018fd12aade99a Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 19 Apr 2021 14:35:36 +0200 Subject: [PATCH] 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. --- ext/spl/spl_dllist.c | 11 +++-------- ext/spl/tests/dllist_013.phpt | 4 +++- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/ext/spl/spl_dllist.c b/ext/spl/spl_dllist.c index da4a67a2df7..ebbcb90cf53 100644 --- a/ext/spl/spl_dllist.c +++ b/ext/spl/spl_dllist.c @@ -168,7 +168,7 @@ static void spl_ptr_llist_unshift(spl_ptr_llist *llist, zval *data) /* {{{ */ elem->prev = NULL; elem->next = llist->head; - ZVAL_COPY_VALUE(&elem->data, data); + ZVAL_COPY(&elem->data, data); SPL_LLIST_RC(elem) = 1; if (llist->head) { @@ -179,8 +179,6 @@ static void spl_ptr_llist_unshift(spl_ptr_llist *llist, zval *data) /* {{{ */ llist->head = elem; 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->next = NULL; - ZVAL_COPY_VALUE(&elem->data, data); + ZVAL_COPY(&elem->data, data); SPL_LLIST_RC(elem) = 1; if (llist->tail) { @@ -201,8 +199,6 @@ static void spl_ptr_llist_push(spl_ptr_llist *llist, zval *data) /* {{{ */ llist->tail = elem; llist->count++; - - Z_TRY_ADDREF(elem->data); } /* }}} */ @@ -1189,7 +1185,6 @@ PHP_METHOD(SplDoublyLinkedList, add) RETURN_THROWS(); } - Z_TRY_ADDREF_P(value); 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 */ spl_ptr_llist_push(intern->llist, value); @@ -1200,7 +1195,7 @@ PHP_METHOD(SplDoublyLinkedList, add) /* Get the element we want to insert before */ 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; /* connect to the neighbours */ elem->next = element; diff --git a/ext/spl/tests/dllist_013.phpt b/ext/spl/tests/dllist_013.phpt index 2226598fd09..2ec8396c0f5 100644 --- a/ext/spl/tests/dllist_013.phpt +++ b/ext/spl/tests/dllist_013.phpt @@ -33,8 +33,9 @@ echo $dll->pop()."\n"; // Test refcounted value $str = "foo"; $str .= "bar"; -$dll->add(0, null); $dll->add(0, $str); +$dll->add(0, $str); +var_dump($dll->shift()); var_dump($dll->shift()); ?> @@ -49,3 +50,4 @@ Exception: SplDoublyLinkedList::add(): Argument #1 ($index) is out of range 2 1 string(6) "foobar" +string(6) "foobar"