Bugs in java.util.ArrayList, java.util.Hashtable and java.io.ByteArrayOutputStream

Ulf Zibis Ulf.Zibis at gmx.de
Tue Mar 9 20:25:28 UTC 2010


Am 09.03.2010 20:18, schrieb Martin Buchholz:
> On Tue, Mar 9, 2010 at 03:59, Ulf Zibis<Ulf.Zibis at gmx.de>  wrote:
>    
>> In PriorityQueue:
>>
>> let's result newCapacity in 0xFFFF.FFFC  =-4
>> then "if (newCapacity - MAX_ARRAY_SIZE>  0)" --->  false
>> then Arrays.copyOf(queue, newCapacity) --->  ArrayIndexOutOfBoundsException
>>      
> How could newCapacity ever become -4?
> Since growth is by 50%.

Oops, I must admit, that I didn't evaluate that.
Many tricks are interwoven here at one place.
I think, those magic should be better commented. Isn't PriorityQueue a 
public API, visible to everybody?
As said, I think Hotspot compiler would be the better place to optimize 
those if...else branches.

>    But even 100% looks safe...
>    

Hm, having oldCapacity = 0x7FFF.FFFE + 100 % makes 0xFFFF.FFFC

>    
>> Am I wrong ?
>>
>> 2.) Why don't you prefer a system-wide constant for MAX_ARRAY_SIZE ???
>>      
> This should never become a public API - it's a bug in the VM.
>
> I prefer the duplication of code to creating a new external dependency.
>    

Good use case for new super package facility.

I can sympathise your reserve. On the other hand ...
- if there is a limit, developers should have a chance, to evaluate 
against it to avoid OutOfMemoryError.
- maybe other VM's have a other/much lower limit, e.g. on small mobile 
systems.
- If the bug would be fixed, who takes care about the "garbage 
collection" in the code base?
- There is many public stuff in sun.misc.VM class, why not 
MAX_ARRAY_SIZE/maxArraySize()?

-Ulf







More information about the core-libs-dev mailing list