druid icon indicating copy to clipboard operation
druid copied to clipboard

Bug in ByteBufferHashTable.java

Open zhangxinyu1 opened this issue 9 years ago • 2 comments

In the method "reset()" of ByteBufferHashTable.java

    maxBuckets = Math.min(tableArenaSize / bucketSizeWithHash, initialBuckets);
    regrowthThreshold = maxSizeForBuckets(maxBuckets);

    if (maxBuckets < 1) {
      throw new IAE(
          "Not enough capacity for even one row! Need[%,d] but have[%,d].",
          bucketSizeWithHash + Ints.BYTES,
          buffer.capacity()
      );
    }

    // Start table part-way through the buffer so the last growth can start from zero and thereby use more space.
    tableStart = tableArenaSize - maxBuckets * bucketSizeWithHash;
    int nextBuckets = maxBuckets * 2;
    while (true) {
      final int nextTableStart = tableStart - nextBuckets * bucketSizeWithHash;
      if (nextTableStart > tableArenaSize / 2) {
        tableStart = nextTableStart;
        nextBuckets = nextBuckets * 2;
      } else {
        break;
      }
    }

When tableArenaSize/bucketSizeWithHash is less than initialBuckets, maxBuckets will be tableArenaSize/bucketSizeWithHash.

Then, tableStart will be 0 for tableStart = tableArenaSize - maxBuckets * bucketSizeWithHash;

The bug is If the value of nextBuckets * bucketSizeWithHash is out of Integer's range, the value nextTableStart will be wrong. The result of condition nextTableStart > tableArenaSize / 2 will be wrong.

So, I think the value of nextBuckets * bucketSizeWithHash should be long, and we should make sure it is less than the maximum integer value.

zhangxinyu1 avatar Nov 14 '17 13:11 zhangxinyu1

Thanks @zhangxinyu1, do you want to make a pull-request to fix this?

leventov avatar Nov 14 '17 18:11 leventov

@leventov Yes, could you please review this pull-request

zhangxinyu1 avatar Nov 15 '17 03:11 zhangxinyu1