diff --git a/src/core/common/linked_list.hpp b/src/core/common/linked_list.hpp index 2f6504497..9d4add2ba 100644 --- a/src/core/common/linked_list.hpp +++ b/src/core/common/linked_list.hpp @@ -199,18 +199,28 @@ public: * * @note This method does not change the popped entry itself, i.e., the popped entry next pointer stays as before. * - * @param[in] aPrevEntry A reference to n previous entry (the entry after this will be popped). - - * @returns The entry that was popped if list is not empty, or NULL if there is no entry after the given one. + * @param[in] aPrevEntry A pointer to a previous entry. If it is not NULL the entry after this will be popped, + * otherwise (if it is NULL) the entry at head of the list is popped. + * + * @returns Pointer to the entry that was popped, or NULL if there is no entry to pop. * */ - Type *PopAfter(Type &aPrevEntry) + Type *PopAfter(Type *aPrevEntry) { - Type *entry = aPrevEntry.GetNext(); + Type *entry; - if (entry != NULL) + if (aPrevEntry == NULL) { - aPrevEntry.SetNext(entry->GetNext()); + entry = Pop(); + } + else + { + entry = aPrevEntry->GetNext(); + + if (entry != NULL) + { + aPrevEntry->SetNext(entry->GetNext()); + } } return entry; diff --git a/src/core/net/netif.cpp b/src/core/net/netif.cpp index b72ac1a06..7b886377f 100644 --- a/src/core/net/netif.cpp +++ b/src/core/net/netif.cpp @@ -404,15 +404,7 @@ otError Netif::UnsubscribeExternalMulticast(const Address &aAddress) VerifyOrExit((entry >= &mExtMulticastAddresses[0]) && (entry < OT_ARRAY_END(mExtMulticastAddresses)), error = OT_ERROR_INVALID_ARGS); - if (last) - { - mMulticastAddresses.PopAfter(*last); - } - else - { - mMulticastAddresses.Pop(); - } - + mMulticastAddresses.PopAfter(last); break; } @@ -528,15 +520,7 @@ otError Netif::RemoveExternalUnicastAddress(const Address &aAddress) VerifyOrExit((entry >= &mExtUnicastAddresses[0]) && (entry < OT_ARRAY_END(mExtUnicastAddresses)), error = OT_ERROR_INVALID_ARGS); - if (last) - { - mUnicastAddresses.PopAfter(*last); - } - else - { - mUnicastAddresses.Pop(); - } - + mUnicastAddresses.PopAfter(last); break; } diff --git a/tests/unit/test_linked_list.cpp b/tests/unit/test_linked_list.cpp index 8516ef436..5fc51f368 100644 --- a/tests/unit/test_linked_list.cpp +++ b/tests/unit/test_linked_list.cpp @@ -132,13 +132,13 @@ void TestLinkedList(void) list.Push(e); VerifyLinkedListContent(&list, &e, &c, &a, &d, &b, NULL); - VerifyOrQuit(list.PopAfter(a) == &d, "LinkedList::PopAfter() failed"); + VerifyOrQuit(list.PopAfter(&a) == &d, "LinkedList::PopAfter() failed"); VerifyLinkedListContent(&list, &e, &c, &a, &b, NULL); - VerifyOrQuit(list.PopAfter(b) == NULL, "LinkedList::PopAfter() failed"); + VerifyOrQuit(list.PopAfter(&b) == NULL, "LinkedList::PopAfter() failed"); VerifyLinkedListContent(&list, &e, &c, &a, &b, NULL); - VerifyOrQuit(list.PopAfter(e) == &c, "LinkedList::PopAfter() failed"); + VerifyOrQuit(list.PopAfter(&e) == &c, "LinkedList::PopAfter() failed"); VerifyLinkedListContent(&list, &e, &a, &b, NULL); list.PushAfter(c, b); @@ -147,8 +147,15 @@ void TestLinkedList(void) list.PushAfter(d, a); VerifyLinkedListContent(&list, &e, &a, &d, &b, &c, NULL); + VerifyOrQuit(list.PopAfter(NULL) == &e, "LinkedList::PopAfter() failed"); + VerifyLinkedListContent(&list, &a, &d, &b, &c, NULL); + + VerifyOrQuit(list.PopAfter(NULL) == &a, "LinkedList::PopAfter() failed"); + VerifyLinkedListContent(&list, &d, &b, &c, NULL); + list.Clear(); VerifyOrQuit(list.IsEmpty(), "LinkedList::IsEmpty() failed after Clear()"); + VerifyOrQuit(list.PopAfter(NULL) == NULL, "LinkedList::PopAfter() failed"); VerifyLinkedListContent(&list, NULL); }