From 425eaa7a6fabb568a50f5647ad58ae7a4431d0f3 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 11 Jul 2019 10:09:45 +0200 Subject: [PATCH] Ensure the hash length (2x) (#897) * Ensure the hash length (2x) * Fix ecpoint --- neo/Cryptography/ECC/ECPoint.cs | 18 ++++++++++++++---- neo/UIntBase.cs | 12 +++++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/neo/Cryptography/ECC/ECPoint.cs b/neo/Cryptography/ECC/ECPoint.cs index 99d2b099e1..0d3ce633bb 100644 --- a/neo/Cryptography/ECC/ECPoint.cs +++ b/neo/Cryptography/ECC/ECPoint.cs @@ -124,13 +124,23 @@ public static ECPoint DeserializeFrom(BinaryReader reader, ECCurve curve) return curve.Infinity; case 0x02: case 0x03: - reader.Read(buffer, 1, expectedLength); - return DecodePoint(buffer.Take(1 + expectedLength).ToArray(), curve); + { + if (reader.Read(buffer, 1, expectedLength) != expectedLength) + { + throw new FormatException(); + } + return DecodePoint(buffer.Take(1 + expectedLength).ToArray(), curve); + } case 0x04: case 0x06: case 0x07: - reader.Read(buffer, 1, expectedLength * 2); - return DecodePoint(buffer, curve); + { + if (reader.Read(buffer, 1, expectedLength * 2) != expectedLength * 2) + { + throw new FormatException(); + } + return DecodePoint(buffer, curve); + } default: throw new FormatException("Invalid point encoding " + buffer[0]); } diff --git a/neo/UIntBase.cs b/neo/UIntBase.cs index 4ab43c83f9..22431c4cae 100644 --- a/neo/UIntBase.cs +++ b/neo/UIntBase.cs @@ -5,7 +5,6 @@ namespace Neo { - /// /// Base class for little-endian unsigned integers. Two classes inherit from this: UInt160 and UInt256. /// Only basic comparison/serialization are proposed for these classes. For arithmetic purposes, use BigInteger class. @@ -15,7 +14,7 @@ public abstract class UIntBase : IEquatable, ISerializable /// /// Storing unsigned int in a little-endian byte array. /// - private byte[] data_bytes; + private readonly byte[] data_bytes; /// /// Number of bytes of the unsigned int. @@ -44,7 +43,10 @@ protected UIntBase(int bytes, byte[] value) /// void ISerializable.Deserialize(BinaryReader reader) { - reader.Read(data_bytes, 0, data_bytes.Length); + if (reader.Read(data_bytes, 0, data_bytes.Length) != data_bytes.Length) + { + throw new FormatException(); + } } /// @@ -53,7 +55,7 @@ void ISerializable.Deserialize(BinaryReader reader) /// public bool Equals(UIntBase other) { - if (ReferenceEquals(other, null)) + if (other is null) return false; if (ReferenceEquals(this, other)) return true; @@ -68,7 +70,7 @@ public bool Equals(UIntBase other) /// public override bool Equals(object obj) { - if (ReferenceEquals(obj, null)) + if (obj is null) return false; if (!(obj is UIntBase)) return false;