Bugs in java.util.ArrayList, java.util.Hashtable and java.io.ByteArrayOutputStream
Kevin L. Stern
kevin.l.stern at gmail.com
Tue Mar 9 10:38:17 UTC 2010
These comparisons are essential to the working of Martin's algorithm. I
found them interesting as well, but notice that when the capacity overflows
these comparisons will always be false. That is to say:
oldCapacity < minCapacity (given, otherwise we would not be resizing)
therefore oldCapacity + (0.5 for ArrayList, else 1) * oldCapacity -
minCapacity < oldCapacity
So if oldCapacity + (0.5 for ArrayList, else 1) * oldCapacity >
Integer.MAX_VALUE, subtracting minCapacity re-overflows back into the
positive number realm.
That being said, and this is a question/comment to all, I want to point out
that this type of code assumes a particular class of orderly overflow
behavior. Is this specified in the Java spec, or will this break on an
obscure machine that does not use, say, two's complement arithmetic?
Regards,
Kevin
2010/3/9 Dmytro Sheyko <dmytro_sheyko at hotmail.com>
> Is there any reason to use comparison like this
>
> if (newCapacity - minCapacity < 0)
>
> if (newCapacity - MAX_ARRAY_SIZE > 0) {
>
> instead of
>
> if (newCapacity < minCapacity)
>
> if (newCapacity > MAX_ARRAY_SIZE) {
>
> Thanks,
> Dmytro
>
> > Date: Mon, 8 Mar 2010 18:10:37 -0800
> > Subject: Re: Bugs in java.util.ArrayList, java.util.Hashtable and
> java.io.ByteArrayOutputStream
> > From: martinrb at google.com
> > To: kevin.l.stern at gmail.com; christopher.hegarty at sun.com;
> alan.bateman at sun.com
> > CC: core-libs-dev at openjdk.java.net
>
> >
> > [Chris or Alan, please review and file a bug]
> >
> > OK, guys,
> >
> > Here's a patch:
> >
> > http://cr.openjdk.java.net/~martin/webrevs/openjdk7/ArrayResize/<http://cr.openjdk.java.net/%7Emartin/webrevs/openjdk7/ArrayResize/>
> >
> > Martin
>
>
> ------------------------------
> Hotmail: Trusted email with powerful SPAM protection. Sign up now.<https://signup.live.com/signup.aspx?id=60969>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20100309/ec479e8c/attachment.html>
More information about the core-libs-dev
mailing list