Bug in Long.parseUnsignedLong

Louis Wasserman lowasser at google.com
Thu Dec 19 18:28:28 UTC 2013


Here's one approach that works: there is overflow iff

compareUnsigned(first, divideUnsigned(MAX_UNSIGNED, radix)) > 0 || (first
== divideUnsigned(MAX_UNSIGNED, radix) && second >
remainderUnsigned(MAX_UNSIGNED, radix));

Since radix <= Character.MAX_RADIX, you can precompute the divides and
remainders in a small table.


On Thu, Dec 19, 2013 at 9:42 AM, Brian Burkhalter <
brian.burkhalter at oracle.com> wrote:

>
> On Dec 19, 2013, at 9:36 AM, Paul Sandoz wrote:
>
> > I think the logic for overflow when using the compareUnsigned is
> incorrect in Long:
> >
> > […]
> >                long result = first * radix + second;
> >                if (compareUnsigned(result, first) < 0) {
>
> I concur and verified that yesterday by replacing the above logic with
> BigInteger equivalents which passed the test.
>
> > My brain is too clogged up with cold at the moment to propose a fix :-(
>
> Hope you recover soon!
>
> Thanks,
>
> Brian




-- 
Louis Wasserman



More information about the core-libs-dev mailing list