This is an automated email from the ASF dual-hosted git repository.

jbertram pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git


The following commit(s) were added to refs/heads/main by this push:
     new 580a230b57 ARTEMIS-5164 Avoiding NPE after duplicated elements on 
LinkedList
580a230b57 is described below

commit 580a230b5745b29ef48ca52c62354e56fb0c2fb1
Author: Clebert Suconic <[email protected]>
AuthorDate: Tue Feb 25 20:32:47 2025 -0500

    ARTEMIS-5164 Avoiding NPE after duplicated elements on LinkedList
---
 .../artemis/utils/collections/LinkedListImpl.java  |  6 ++---
 .../artemis/tests/unit/util/LinkedListTest.java    | 28 ++++++++++++++++++++++
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LinkedListImpl.java
 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LinkedListImpl.java
index af1e50db95..7d554bddc6 100644
--- 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LinkedListImpl.java
+++ 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LinkedListImpl.java
@@ -212,7 +212,7 @@ public class LinkedListImpl<E> implements LinkedList<E> {
          logger.trace("adding head as there are no elements {}", e);
          addHead(e);
       } else {
-         if (comparator.compare(head.next.val(), e) < 0) {
+         if (comparator.compare(head.next.val(), e) <= 0) {
             if (logger.isTraceEnabled()) {
                logger.trace("addHead as e={} and head={}", e, head.next.val());
             }
@@ -270,7 +270,7 @@ public class LinkedListImpl<E> implements LinkedList<E> {
    protected boolean scanRight(Node<E> position, E e) {
       Node<E> fetching = position.next;
       while (fetching != null) {
-         if (comparator.compare(fetching.val(), e) < 0) {
+         if (comparator.compare(fetching.val(), e) <= 0) {
             addAfter(position, e);
             return true;
          }
@@ -283,7 +283,7 @@ public class LinkedListImpl<E> implements LinkedList<E> {
    protected boolean scanLeft(Node<E> position, E e) {
       Node<E> fetching = position.prev;
       while (fetching != null) {
-         if (comparator.compare(fetching.val(), e) > 0) {
+         if (comparator.compare(fetching.val(), e) >= 0) {
             addAfter(fetching, e);
             return true;
          }
diff --git 
a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/LinkedListTest.java
 
b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/LinkedListTest.java
index a3f20230ea..af5a513d3f 100644
--- 
a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/LinkedListTest.java
+++ 
b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/LinkedListTest.java
@@ -160,6 +160,34 @@ public class LinkedListTest extends ActiveMQTestBase {
       }
    }
 
+   @Test
+   public void testDuplicateSorted() {
+      list.addSorted(1);
+      list.addSorted(2);
+      list.addSorted(1);
+
+      try (LinkedListIterator<Integer> listIterator = list.iterator()) {
+         assertEquals(1, listIterator.next().intValue());
+         assertEquals(1, listIterator.next().intValue());
+         assertEquals(2, listIterator.next().intValue());
+      }
+
+      list.clear();
+
+      list.addSorted(10);
+      list.addSorted(2);
+      list.addSorted(1);
+      list.addSorted(2);
+
+      try (LinkedListIterator<Integer> listIterator = list.iterator()) {
+         assertEquals(1, listIterator.next().intValue());
+         assertEquals(2, listIterator.next().intValue());
+         assertEquals(2, listIterator.next().intValue());
+         assertEquals(10, listIterator.next().intValue());
+      }
+
+   }
+
    @Test
    public void randomSorted() {
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information, visit: https://activemq.apache.org/contact


Reply via email to