hiero-ledger / hiero-ledger/hiero-consensus-node

Tipset throws IndexOutOfBoundsException if the address book becomes smaller

Open
#15,469 4 comments 0 reactions 0 assignees View on GitHub
Platform
Dominant language
Java
Stars
406
Forks
226
Avg merge
3d 4h
Merged PRs (30d)
210

Description

### Description

While reviewing this method:

```
public static Tipset merge(final List tipsets) {
Objects.requireNonNull(tipsets, "tipsets must not be null");
if (tipsets.isEmpty()) {
throw new IllegalArgumentException("Cannot merge an empty list of tipsets");
}

final var firstTipset = tipsets.getFirst();
final int length = firstTipset.tips.length;
final Tipset newTipset = buildEmptyTipset(firstTipset);

for (int index = 0; index < length; index++) {
long max = UNDEFINED;
for (final Tipset tipSet : tipsets) {
max = Math.max(max, tipSet.tips[index]);
}
newTipset.tips[index] = max;
}

return newTipset;
}
```

It appears an IOOBE will be thrown on `Math.max(max, tipSet.tips[index]);` if the address book has become smaller. When merging tipsets, this code takes the first tipset in the list, and uses it for the length. Any subsequent tipset in the merge that is less than this length will not have an index position of "index".

It isn't clear why the first tipset in the list is given preference, or why the new merged tipset should use its length.

### Steps to reproduce

A unit test to this effect has not been written, but could be.

### Additional context

_No response_

### Hedera network

_No response_

### Version

develop

### Operating system

None

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.