Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
Add some exception messages (#389)
Browse files Browse the repository at this point in the history
* Add some exception messages

* Code style changes
  • Loading branch information
shargon authored Jan 15, 2021
1 parent 097b758 commit eaf8d30
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/neo-vm/EvaluationStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ IEnumerator IEnumerable.GetEnumerator()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Insert(int index, StackItem item)
{
if (index > innerList.Count) throw new InvalidOperationException();
if (index > innerList.Count) throw new InvalidOperationException($"Insert out of bounds: {index}/{innerList.Count}");
innerList.Insert(innerList.Count - index, item);
referenceCounter.AddStackReference(item);
}
Expand All @@ -68,11 +68,11 @@ internal void MoveTo(EvaluationStack stack, int count = -1)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public StackItem Peek(int index = 0)
{
if (index >= innerList.Count) throw new InvalidOperationException();
if (index >= innerList.Count) throw new InvalidOperationException($"Peek out of bounds: {index}/{innerList.Count}");
if (index < 0)
{
index += innerList.Count;
if (index < 0) throw new InvalidOperationException();
if (index < 0) throw new InvalidOperationException($"Peek out of bounds: {index}/{innerList.Count}");
}
return innerList[innerList.Count - index - 1];
}
Expand Down Expand Up @@ -118,7 +118,7 @@ internal T Remove<T>(int index) where T : StackItem
throw new ArgumentOutOfRangeException(nameof(index));
}
index = innerList.Count - index - 1;
if (!(innerList[index] is T item))
if (innerList[index] is not T item)
throw new InvalidCastException($"The item can't be casted to type {typeof(T)}");
innerList.RemoveAt(index);
referenceCounter.RemoveStackReference(item);
Expand Down
22 changes: 8 additions & 14 deletions src/neo-vm/ExecutionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ private void ExecuteInstruction()
if (context_pop.EvaluationStack != stack_eval)
{
if (context_pop.RVCount >= 0 && context_pop.EvaluationStack.Count != context_pop.RVCount)
throw new InvalidOperationException();
throw new InvalidOperationException("RVCount doesn't match with EvaluationStack");
context_pop.EvaluationStack.CopyTo(stack_eval);
}
if (InvocationStack.Count == 0)
Expand Down Expand Up @@ -1046,19 +1046,13 @@ private void ExecuteInstruction()
}
case OpCode.VALUES:
{
IEnumerable<StackItem> values;
var x = Pop();
switch (x)
IEnumerable<StackItem> values = x switch
{
case VMArray array:
values = array;
break;
case Map map:
values = map.Values;
break;
default:
throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {x.Type}");
}
VMArray array => array,
Map map => map.Values,
_ => throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {x.Type}"),
};
VMArray newArray = new VMArray(ReferenceCounter);
foreach (StackItem item in values)
if (item is Struct s)
Expand Down Expand Up @@ -1145,7 +1139,7 @@ private void ExecuteInstruction()
int index = key.ToInt32();
if (index < 0 || index >= buffer.Size)
throw new InvalidOperationException($"The value {index} is out of range.");
if (!(value is PrimitiveType p))
if (value is not PrimitiveType p)
throw new InvalidOperationException($"Value must be a primitive type in {instruction.OpCode}");
int b = p.ToInt32();
if (b < sbyte.MinValue || b > byte.MaxValue)
Expand Down Expand Up @@ -1381,7 +1375,7 @@ private void HandleException()
protected virtual void LoadContext(ExecutionContext context)
{
if (InvocationStack.Count >= Limits.MaxInvocationStackSize)
throw new InvalidOperationException();
throw new InvalidOperationException($"MaxInvocationStackSize exceed: {InvocationStack.Count}");
InvocationStack.Push(context);
if (EntryContext is null) EntryContext = context;
CurrentContext = context;
Expand Down
2 changes: 1 addition & 1 deletion src/neo-vm/Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ internal Instruction(byte[] script, int ip)
{
ip += operandSizePrefix;
if (ip + operandSize > script.Length)
throw new InvalidOperationException();
throw new InvalidOperationException($"Instrucion out of bounds. InstructionPointer: {ip}, operandSize: {operandSize}, length: {script.Length}");
Operand = new ReadOnlyMemory<byte>(script, ip, operandSize);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/neo-vm/ReferenceCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private class Entry
internal void AddReference(StackItem referred, CompoundType parent)
{
references_count++;
if (!(referred is CompoundType compound)) return;
if (referred is not CompoundType compound) return;
if (!counter.TryGetValue(compound, out Entry tracing))
{
tracing = new Entry();
Expand All @@ -46,7 +46,7 @@ internal void AddReference(StackItem referred, CompoundType parent)
internal void AddStackReference(StackItem referred)
{
references_count++;
if (!(referred is CompoundType compound)) return;
if (referred is not CompoundType compound) return;
if (counter.TryGetValue(compound, out Entry entry))
entry.StackReferences++;
else
Expand Down Expand Up @@ -108,7 +108,7 @@ internal int CheckZeroReferred()
internal void RemoveReference(StackItem referred, CompoundType parent)
{
references_count--;
if (!(referred is CompoundType compound)) return;
if (referred is not CompoundType compound) return;
Entry entry = counter[compound];
entry.ObjectReferences[parent] -= 1;
if (entry.StackReferences == 0)
Expand All @@ -118,7 +118,7 @@ internal void RemoveReference(StackItem referred, CompoundType parent)
internal void RemoveStackReference(StackItem referred)
{
references_count--;
if (!(referred is CompoundType item_compound)) return;
if (referred is not CompoundType item_compound) return;
if (--counter[item_compound].StackReferences == 0)
zero_referred.Add(item_compound);
}
Expand Down
4 changes: 2 additions & 2 deletions src/neo-vm/Types/ByteString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override bool Equals(StackItem other)
if (Size > MaxComparableSize)
throw new InvalidOperationException("The operand exceeds the maximum comparable size.");
if (ReferenceEquals(this, other)) return true;
if (!(other is ByteString b)) return false;
if (other is not ByteString b) return false;
if (b.Size > MaxComparableSize)
throw new InvalidOperationException("The operand exceeds the maximum comparable size.");
return GetSpan().SequenceEqual(b.GetSpan());
Expand All @@ -50,7 +50,7 @@ public override int GetHashCode()

public override BigInteger GetInteger()
{
if (Size > Integer.MaxSize) throw new InvalidCastException();
if (Size > Integer.MaxSize) throw new InvalidCastException($"MaxSize exceed: {Size}");
return new BigInteger(GetSpan());
}

Expand Down
2 changes: 1 addition & 1 deletion src/neo-vm/Types/Integer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Integer(BigInteger value)
else
{
Size = value.GetByteCount();
if (Size > MaxSize) throw new ArgumentException();
if (Size > MaxSize) throw new ArgumentException($"MaxSize exceed: {Size}");
}
this.value = value;
}
Expand Down
4 changes: 2 additions & 2 deletions src/neo-vm/Types/InteropInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class InteropInterface : StackItem

public InteropInterface(object value)
{
_object = value ?? throw new ArgumentException();
_object = value ?? throw new ArgumentNullException(nameof(value));
}

public override bool Equals(StackItem other)
Expand All @@ -35,7 +35,7 @@ public override int GetHashCode()
public override T GetInterface<T>()
{
if (_object is T t) return t;
throw new InvalidCastException();
throw new InvalidCastException($"The item can't be casted to type {typeof(T)}");
}
}
}
2 changes: 1 addition & 1 deletion src/neo-vm/Types/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public StackItem this[PrimitiveType key]
set
{
if (key.Size > MaxKeySize)
throw new ArgumentException();
throw new ArgumentException($"MaxKeySize exceed: {key.Size}");
if (ReferenceCounter != null)
{
if (dictionary.TryGetValue(key, out StackItem old_value))
Expand Down
2 changes: 1 addition & 1 deletion src/neo-vm/Types/Null.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal Null() { }
public override StackItem ConvertTo(StackItemType type)
{
if (type == StackItemType.Any || !Enum.IsDefined(typeof(StackItemType), type))
throw new InvalidCastException();
throw new InvalidCastException($"Type can't be converted to StackItemType: {type}");
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/neo-vm/Types/PrimitiveType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public sealed override ReadOnlySpan<byte> GetSpan()
public int ToInt32()
{
BigInteger i = GetInteger();
if (i < int.MinValue || i > int.MaxValue) throw new InvalidCastException();
if (i < int.MinValue || i > int.MaxValue) throw new InvalidCastException($"{i} can't be converted to a valid integer value");
return (int)i;
}

Expand Down
4 changes: 2 additions & 2 deletions src/neo-vm/Types/Struct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override StackItem ConvertTo(StackItemType type)

public override bool Equals(StackItem other)
{
if (!(other is Struct s)) return false;
if (other is not Struct s) return false;
Stack<StackItem> stack1 = new Stack<StackItem>();
Stack<StackItem> stack2 = new Stack<StackItem>();
stack1.Push(this);
Expand All @@ -65,7 +65,7 @@ public override bool Equals(StackItem other)
if (a is Struct sa)
{
if (ReferenceEquals(a, b)) continue;
if (!(b is Struct sb)) return false;
if (b is not Struct sb) return false;
if (sa.Count != sb.Count) return false;
foreach (StackItem item in sa)
stack1.Push(item);
Expand Down
4 changes: 2 additions & 2 deletions tests/neo-vm.Tests/UtStackItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void HashCodeTest()

Assert.IsTrue(itemA.GetHashCode() == itemB.GetHashCode());

var script = new Script(new byte[0]);
var script = new Script(System.Array.Empty<byte>());
itemA = new Pointer(script, 123);
itemB = new Pointer(script, 123);
itemC = new Pointer(script, 1234);
Expand All @@ -71,7 +71,7 @@ public void HashCodeTest()
[TestMethod]
public void NullTest()
{
StackItem nullItem = new byte[0];
StackItem nullItem = System.Array.Empty<byte>();
Assert.AreNotEqual(StackItem.Null, nullItem);

nullItem = new Null();
Expand Down
2 changes: 0 additions & 2 deletions tests/neo-vm.Tests/UtStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public void Clone()
}

[TestMethod]
#pragma warning disable xUnit1024 // Test methods cannot have overloads
public void Equals()
#pragma warning restore xUnit1024 // Test methods cannot have overloads
{
Struct s1 = new Struct { 1, new Struct { 2 } };
Struct s2 = new Struct { 1, new Struct { 2 } };
Expand Down
2 changes: 1 addition & 1 deletion tests/neo-vm.Tests/UtUnsafe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class UtUnsafe
[TestMethod]
public void NotZero()
{
Assert.IsFalse(Unsafe.NotZero(new byte[0]));
Assert.IsFalse(Unsafe.NotZero(System.Array.Empty<byte>()));
Assert.IsFalse(Unsafe.NotZero(new byte[4]));
Assert.IsFalse(Unsafe.NotZero(new byte[8]));
Assert.IsFalse(Unsafe.NotZero(new byte[11]));
Expand Down

0 comments on commit eaf8d30

Please sign in to comment.