From 1c326ca37fb514ed1afb3c57862d131ae976fe03 Mon Sep 17 00:00:00 2001 From: Erik van den Brink Date: Tue, 26 Nov 2019 04:58:50 +0100 Subject: [PATCH] apply strict enum checking (#1254) --- src/neo/Consensus/ConsensusMessage.cs | 2 ++ src/neo/Network/P2P/Payloads/TransactionAttribute.cs | 4 +++- src/neo/Wallets/SQLite/VerificationContract.cs | 8 +++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/neo/Consensus/ConsensusMessage.cs b/src/neo/Consensus/ConsensusMessage.cs index 0bca5534e1..856ccdc72b 100644 --- a/src/neo/Consensus/ConsensusMessage.cs +++ b/src/neo/Consensus/ConsensusMessage.cs @@ -14,6 +14,8 @@ public abstract class ConsensusMessage : ISerializable protected ConsensusMessage(ConsensusMessageType type) { + if (!Enum.IsDefined(typeof(ConsensusMessageType), type)) + throw new ArgumentOutOfRangeException(nameof(type)); this.Type = type; } diff --git a/src/neo/Network/P2P/Payloads/TransactionAttribute.cs b/src/neo/Network/P2P/Payloads/TransactionAttribute.cs index fa92010004..d4c31b2e49 100644 --- a/src/neo/Network/P2P/Payloads/TransactionAttribute.cs +++ b/src/neo/Network/P2P/Payloads/TransactionAttribute.cs @@ -37,7 +37,9 @@ public JObject ToJson() public static TransactionAttribute FromJson(JObject json) { TransactionAttribute transactionAttribute = new TransactionAttribute(); - transactionAttribute.Usage = (TransactionAttributeUsage)(byte.Parse(json["usage"].AsString())); + transactionAttribute.Usage = (TransactionAttributeUsage)byte.Parse(json["usage"].AsString()); + if (!Enum.IsDefined(typeof(TransactionAttributeUsage), transactionAttribute.Usage)) + throw new ArgumentException(); transactionAttribute.Data = Convert.FromBase64String(json["data"].AsString()); return transactionAttribute; } diff --git a/src/neo/Wallets/SQLite/VerificationContract.cs b/src/neo/Wallets/SQLite/VerificationContract.cs index 587a0fe468..b195f50ca5 100644 --- a/src/neo/Wallets/SQLite/VerificationContract.cs +++ b/src/neo/Wallets/SQLite/VerificationContract.cs @@ -14,7 +14,13 @@ public class VerificationContract : SmartContract.Contract, IEquatable(); - ParameterList = reader.ReadVarBytes().Select(p => (ContractParameterType)p).ToArray(); + ParameterList = reader.ReadVarBytes().Select(p => + { + var ret = (ContractParameterType)p; + if (!Enum.IsDefined(typeof(ContractParameterType), ret)) + throw new FormatException(); + return ret; + }).ToArray(); Script = reader.ReadVarBytes(); }