Skip to content

Commit

Permalink
Add safety checks for ConsensusContext Deserialization (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon authored and jsolman committed Mar 5, 2019
1 parent 1c29dee commit eb26278
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 5 additions & 5 deletions neo/Consensus/ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void Deserialize(BinaryReader reader)
TransactionHashes = reader.ReadSerializableArray<UInt256>();
if (TransactionHashes.Length == 0)
TransactionHashes = null;
Transaction[] transactions = new Transaction[reader.ReadVarInt()];
Transaction[] transactions = new Transaction[reader.ReadVarInt(Block.MaxTransactionsPerBlock)];
if (transactions.Length == 0)
{
Transactions = null;
Expand All @@ -90,16 +90,16 @@ public void Deserialize(BinaryReader reader)
transactions[i] = Transaction.DeserializeFrom(reader);
Transactions = transactions.ToDictionary(p => p.Hash);
}
PreparationPayloads = new ConsensusPayload[reader.ReadVarInt()];
PreparationPayloads = new ConsensusPayload[reader.ReadVarInt(Blockchain.MaxValidators)];
for (int i = 0; i < PreparationPayloads.Length; i++)
PreparationPayloads[i] = reader.ReadBoolean() ? reader.ReadSerializable<ConsensusPayload>() : null;
CommitPayloads = new ConsensusPayload[reader.ReadVarInt()];
CommitPayloads = new ConsensusPayload[reader.ReadVarInt(Blockchain.MaxValidators)];
for (int i = 0; i < CommitPayloads.Length; i++)
CommitPayloads[i] = reader.ReadBoolean() ? reader.ReadSerializable<ConsensusPayload>() : null;
ChangeViewPayloads = new ConsensusPayload[reader.ReadVarInt()];
ChangeViewPayloads = new ConsensusPayload[reader.ReadVarInt(Blockchain.MaxValidators)];
for (int i = 0; i < ChangeViewPayloads.Length; i++)
ChangeViewPayloads[i] = reader.ReadBoolean() ? reader.ReadSerializable<ConsensusPayload>() : null;
LastChangeViewPayloads = new ConsensusPayload[reader.ReadVarInt()];
LastChangeViewPayloads = new ConsensusPayload[reader.ReadVarInt(Blockchain.MaxValidators)];
for (int i = 0; i < LastChangeViewPayloads.Length; i++)
LastChangeViewPayloads[i] = reader.ReadBoolean() ? reader.ReadSerializable<ConsensusPayload>() : null;
}
Expand Down
4 changes: 3 additions & 1 deletion neo/Consensus/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public static void Save(this IConsensusContext context, Store store)
public static bool Load(this IConsensusContext context, Store store)
{
byte[] data = store.Get(CN_Context, new byte[0]);
if (data is null) return false;

if (data is null || data.Length == 0) return false;

using (MemoryStream ms = new MemoryStream(data, false))
using (BinaryReader reader = new BinaryReader(ms))
{
Expand Down

0 comments on commit eb26278

Please sign in to comment.