This would only partially solve the problem.  In general a circular reference, 
in any object, will demonstrate the same problem.
 
-----Original Message-----
From: Facundo Molina <fmol...@dc.exa.unrc.edu.ar> 
Sent: Wednesday, December 4, 2019 8:19 AM
To: user@commons.apache.org
Subject: [collections] about a possible issue with AbstractLinkedList.hashCode()

Dear all,

My name is Facundo, and I'm currently working on automated test generation.
As part of an experiment, I came across a potential issue with 
org.apache.commons.collections4.list.AbstractLinkedList.hashCode. The code of 
this method is:

  @Override

  public int hashCode() {

    int hashCode = 1;

    for (final E e : this) {

      hashCode = 31 * hashCode + (e == null ? 0 : e.hashCode());

    }

    return hashCode;

  }


The problem may arise when generating a collection of Object, and inserting the 
same collection as an element of itself. For example, consider the following 
test case using the class NodeCachingLinkedList which extends
AbstractLinkedList:

  @Test

  public void test0() {

    NodeCachingLinkedList<Object> nodeCachingLinkedListGA0 = new 
NodeCachingLinkedList<Object>();

    nodeCachingLinkedListGA0.addFirst(nodeCachingLinkedListGA0);

    nodeCachingLinkedListGA0.hashCode();

  }


This will result in an infinite recursive call, leading to a stack overflow 
exception.

I would like to know if you consider this to be an issue. A possible solution 
would be the following:

  @Override

  public int hashCode() {

    int hashCode = 1;

    for (final E e : this) {

      hashCode = 31 * hashCode + (e == null || e == this ? 0 : e .hashCode());

    }

    return hashCode;

  }


Please let me know what you think about it.

Best regards,
Facundo

--
Facundo Molina

Reply via email to