Hi, Sian.

It seems that this patch is a workaround about the testcase. What if our
user really can malloc Integer.MAX_VALUE memorys to hold the hashmap? And
the size is also not right if we just negate the overflow one, it will cause
the potential problem.

The root cause of this problem is because we just use one buffer to hold
both key and value. What about using two buffers, split the key and value
pair into seperate buffers?

On Fri, May 15, 2009 at 5:08 PM, <[email protected]> wrote:

> Author: sjanuary
> Date: Fri May 15 09:08:28 2009
> New Revision: 775060
>
> URL: http://svn.apache.org/viewvc?rev=775060&view=rev
> Log:
> Apply patch for HARMONY-6204 ([classlib][luni]
> java.util.IdentityHashMap.<init>(BigNumber) throws a
> NegativeArraySizeException while RI throws OutOfMemoryError)
>
> Modified:
>
>  
> harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/IdentityHashMap.java
>
>  
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/IdentityHashMap2Test.java
>
> Modified:
> harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/IdentityHashMap.java
> URL:
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/IdentityHashMap.java?rev=775060&r1=775059&r2=775060&view=diff
>
> ==============================================================================
> ---
> harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/IdentityHashMap.java
> (original)
> +++
> harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/IdentityHashMap.java
> Fri May 15 09:08:28 2009
> @@ -267,7 +267,10 @@
>     }
>
>     private int computeElementArraySize() {
> -        return (int) (((long) threshold * 10000) / loadFactor) * 2;
> +        int arraySize = (int) (((long) threshold * 10000) / loadFactor) *
> 2;
> +        // ensure arraySize is positive, the above cast from long to int
> type
> +        // leads to overflow and negative arraySize if threshold is too
> big
> +        return arraySize < 0 ? -arraySize : arraySize;
>     }
>
>     /**
>
> Modified:
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/IdentityHashMap2Test.java
> URL:
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/IdentityHashMap2Test.java?rev=775060&r1=775059&r2=775060&view=diff
>
> ==============================================================================
> ---
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/IdentityHashMap2Test.java
> (original)
> +++
> harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/IdentityHashMap2Test.java
> Fri May 15 09:08:28 2009
> @@ -115,6 +115,15 @@
>         assertEquals("Size should be 0", 0, hm2.size());
>        }
>
> +    public void test_IdentityHashMap_Constructor_BigSize() {
> +        try {
> +            new IdentityHashMap(Integer.MAX_VALUE);
> +            fail("should throw OutOfMemoryError");
> +        } catch (OutOfMemoryError e) {
> +            // Expected
> +        }
> +    }
> +
>        /**
>         * @tests java.util.IdentityHashMap#clear()
>         */
>
>
>


-- 
Yours sincerely,
Charles Lee

Reply via email to