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

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 245d8dffc EqualsBuilder.reflectionEquals() array branch missing cycle 
guard causes (#1653)
245d8dffc is described below

commit 245d8dffcdab9ba3286b6493da595e28094d42a9
Author: Gary Gregory <[email protected]>
AuthorDate: Sun May 17 17:07:22 2026 -0400

    EqualsBuilder.reflectionEquals() array branch missing cycle guard causes 
(#1653)
    
    StackOverflow on self-referential Object arrays
---
 .../commons/lang3/builder/EqualsBuilder.java       | 38 ++++++++------
 .../EqualsBuilderReflectionEqualsCycleTest.java    | 61 ++++++++++++++++++++++
 2 files changed, 82 insertions(+), 17 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java 
b/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java
index 1bb5ed61d..c478ff335 100644
--- a/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java
+++ b/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java
@@ -26,7 +26,6 @@
 
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.ClassUtils;
-import org.apache.commons.lang3.builder.AbstractReflection.AbstractBuilder;
 import org.apache.commons.lang3.tuple.Pair;
 
 /**
@@ -171,7 +170,7 @@ static Set<Pair<IDKey, IDKey>> getRegistry() {
      * <p>
      * Used by the reflection methods to avoid infinite loops.
      * Objects might be swapped therefore a check is needed if the object pair
-     * is registered in given or swapped order.
+     * is registered in the given or swapped order.
      * </p>
      *
      * @param lhs {@code this} object to lookup in registry
@@ -409,7 +408,7 @@ public EqualsBuilder() {
         bypassReflectionClasses.add(String.class); //hashCode field being lazy 
but not transient
     }
 
-    private EqualsBuilder(Builder builder) {
+    private EqualsBuilder(final Builder builder) {
         super(builder);
     }
 
@@ -793,24 +792,29 @@ public EqualsBuilder append(final Object lhs, final 
Object rhs) {
      * @return {@code this} instance.
      */
     public EqualsBuilder append(final Object[] lhs, final Object[] rhs) {
-        if (!isEquals) {
-            return this;
-        }
-        if (lhs == rhs) {
+        if (!isEquals || isRegistered(lhs, rhs)) {
             return this;
         }
-        if (lhs == null || rhs == null) {
-            setEquals(false);
-            return this;
-        }
-        if (lhs.length != rhs.length) {
-            setEquals(false);
+        try {
+            register(lhs, rhs);
+            if (lhs == rhs) {
+                return this;
+            }
+            if (lhs == null || rhs == null) {
+                setEquals(false);
+                return this;
+            }
+            if (lhs.length != rhs.length) {
+                setEquals(false);
+                return this;
+            }
+            for (int i = 0; i < lhs.length && isEquals; ++i) {
+                append(lhs[i], rhs[i]);
+            }
             return this;
+        } finally {
+            unregister(lhs, rhs);
         }
-        for (int i = 0; i < lhs.length && isEquals; ++i) {
-            append(lhs[i], rhs[i]);
-        }
-        return this;
     }
 
     /**
diff --git 
a/src/test/java/org/apache/commons/lang3/builder/EqualsBuilderReflectionEqualsCycleTest.java
 
b/src/test/java/org/apache/commons/lang3/builder/EqualsBuilderReflectionEqualsCycleTest.java
new file mode 100644
index 000000000..d0192c4e4
--- /dev/null
+++ 
b/src/test/java/org/apache/commons/lang3/builder/EqualsBuilderReflectionEqualsCycleTest.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.lang3.builder;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link EqualsBuilder#reflectionEquals(Object, Object, String...)}.
+ * <p>
+ * reflectionEquals array fix enables cyclic-array.
+ * </p>
+ * <p>
+ * Pre-patch: Object[] elements containing themselves cause StackOverflowError 
when compared via EqualsBuilder.reflectionEquals because the array branch in
+ * reflectionAppend calls append(lhs, rhs) which recurses without cycle check.
+ * </p>
+ * <p>
+ * Post-patch: the arrays are registered before recursing so cycles are 
detected and the comparison terminates (returning false).
+ * </p>
+ */
+class EqualsBuilderReflectionEqualsCycleTest {
+
+    @Test
+    void testSelfReferentialObjectArrays() {
+        final Object[] a = new Object[1];
+        final Object[] b = new Object[1];
+        a[0] = a;
+        b[0] = b;
+        // Pre-patch: StackOverflowError; Post-patch: terminates without error.
+        // With cycle detection, comparing a[0]=a vs b[0]=b sees (a,b) already 
registered
+        // and treats the cycle as equal, so the overall result is true 
(structurally isomorphic).
+        // The key assertion is that NO StackOverflowError is thrown.
+        assertTrue(EqualsBuilder.reflectionEquals(a, b));
+    }
+
+    @Test
+    void testCrossReferentialObjectArrays() {
+        final Object[] a = new Object[1];
+        final Object[] b = new Object[1];
+        // a[0] -> b, b[0] -> a: mutual cycle
+        a[0] = b;
+        b[0] = a;
+        assertTrue(EqualsBuilder.reflectionEquals(a, b));
+    }
+}

Reply via email to