Copilot commented on code in PR #645:
URL: 
https://github.com/apache/commons-configuration/pull/645#discussion_r3581018603


##########
src/main/java/org/apache/commons/configuration2/convert/AbstractListDelimiterHandler.java:
##########
@@ -42,24 +43,23 @@ public abstract class AbstractListDelimiterHandler 
implements ListDelimiterHandl
     static Collection<?> flatten(final ListDelimiterHandler handler, final 
Object value, final int limit, final Set<Object> dejaVu) {
         if (value instanceof String) {
             return handler.split((String) value, true);
+        } else if (!isRecursiveContainer(value)) {
+            return value != null ? Collections.singletonList(value) : 
Collections.emptyList();
         }
         dejaVu.add(value);
         final Collection<Object> result = new LinkedList<>();
-        if (value instanceof Path) {
-            // Don't handle as an Iterable.
-            result.add(value);
-        } else if (value instanceof Iterable) {
-            flattenIterator(handler, result, ((Iterable<?>) value).iterator(), 
limit, dejaVu);
-        } else if (value instanceof Iterator) {
-            flattenIterator(handler, result, (Iterator<?>) value, limit, 
dejaVu);
-        } else if (value != null) {
-            if (value.getClass().isArray()) {
+        try {
+            if (value instanceof Iterable) {
+                flattenIterator(handler, result, ((Iterable<?>) 
value).iterator(), limit, dejaVu);

Review Comment:
   In the array branch, recursion currently calls `handler.flatten(...)`, which 
allocates a *new* Identity-based deja-vu set each time (via the default 
interface method) and therefore breaks cycle detection for arrays. Also, 
because `dejaVu.add(value)` is unconditional, a self-referential array (or an 
array element that back-references an ancestor container) can recurse 
indefinitely.
   
   Consider (1) short-circuiting when `value` is already in `dejaVu`, and (2) 
recursing via the static helper so the same `dejaVu` is preserved throughout 
the traversal.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to