BigInteger::getBitRange() broken when value = 0

So, try this in a default project, right in the initialize() method:

BigInteger bi;
auto range = bi.getBitRange(128, 128);

it crashes because:

BigInteger BigInteger::getBitRange (int startBit, int numBits) const
{
    BigInteger r;
    numBits = jmin (numBits, getHighestBit() + 1 - startBit);

when the BigInteger is freshly initialized with 0, getHighestBit() returns -1, which makes the result of that equation:

numBits = jmin(128, -1 + 1 - 128);

a.k.a

numBits = jmin(128, -128);

a.k.a

numBits = -128

Suggested solution:

numBits = jmax(0, jmin(numBits, getHighestBit() + 1 - startBit) );

not sure if this was fixed in Juce5, i’m still on v4.3.1
shout out to Princess17b29a in ##c++-general on Freenode IRC for helping solve that one.

Which raises the philosophical question, if a BigInteger with a range of 0, i.e. having no valid item, should be allowed to exist…

1 Like