Fix index out of bound issues

At Capabilities I got an exception after going back from a new dev
branch to master and getting p2p network data from nodes still running
on the dev branch as that newly added capability was outside of the
existing scope.

I found another potential issue with DaoPhase.Phase and added a check
there as well.
This commit is contained in:
Manfred Karrer 2019-03-10 22:40:26 -05:00
parent 257550ac81
commit 9ed109330a
No known key found for this signature in database
GPG key ID: 401250966A6B2C46
2 changed files with 14 additions and 2 deletions

View file

@ -96,7 +96,10 @@ public class Capabilities {
* @return a {@link Capabilities} object
*/
public static Capabilities fromIntList(List<Integer> capabilities) {
return new Capabilities(capabilities.stream().map(integer -> Capability.values()[integer]).collect(Collectors.toSet()));
return new Capabilities(capabilities.stream()
.filter(integer -> integer < Capability.values().length)
.map(integer -> Capability.values()[integer])
.collect(Collectors.toSet()));
}
@Override

View file

@ -26,6 +26,7 @@ import io.bisq.generated.protobuffer.PB;
import java.util.Objects;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.concurrent.Immutable;
@ -35,6 +36,7 @@ import javax.annotation.concurrent.Immutable;
*/
@Immutable
@Value
@Slf4j
public class DaoPhase implements PersistablePayload, ImmutableDaoStateModel {
/**
@ -77,7 +79,14 @@ public class DaoPhase implements PersistablePayload, ImmutableDaoStateModel {
}
public static DaoPhase fromProto(PB.DaoPhase proto) {
return new DaoPhase(Phase.values()[proto.getPhaseOrdinal()], proto.getDuration());
int ordinal = proto.getPhaseOrdinal();
if (ordinal >= Phase.values().length) {
log.warn("We tried to access a ordinal outside of the DaoPhase.Phase enum bounds and set it to " +
"UNDEFINED. ordinal={}", ordinal);
return new DaoPhase(Phase.UNDEFINED, 0);
}
return new DaoPhase(Phase.values()[ordinal], proto.getDuration());
}