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

Add SafeReadBytes from VM #71

Merged
merged 7 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/neo-vm/ExecutionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void Execute()
private void ExecuteOp(OpCode opcode, ExecutionContext context)
{
if (opcode >= OpCode.PUSHBYTES1 && opcode <= OpCode.PUSHBYTES75)
context.EvaluationStack.Push(context.OpReader.ReadBytes((byte)opcode));
context.EvaluationStack.Push(context.OpReader.SafeReadBytes((byte)opcode));
else
switch (opcode)
{
Expand All @@ -67,13 +67,13 @@ private void ExecuteOp(OpCode opcode, ExecutionContext context)
context.EvaluationStack.Push(new byte[0]);
break;
case OpCode.PUSHDATA1:
context.EvaluationStack.Push(context.OpReader.ReadBytes(context.OpReader.ReadByte()));
context.EvaluationStack.Push(context.OpReader.SafeReadBytes(context.OpReader.ReadByte()));
break;
case OpCode.PUSHDATA2:
context.EvaluationStack.Push(context.OpReader.ReadBytes(context.OpReader.ReadUInt16()));
context.EvaluationStack.Push(context.OpReader.SafeReadBytes(context.OpReader.ReadUInt16()));
break;
case OpCode.PUSHDATA4:
context.EvaluationStack.Push(context.OpReader.ReadBytes(context.OpReader.ReadInt32()));
context.EvaluationStack.Push(context.OpReader.SafeReadBytes(context.OpReader.ReadInt32()));
break;
case OpCode.PUSHM1:
case OpCode.PUSH1:
Expand Down Expand Up @@ -166,7 +166,7 @@ private void ExecuteOp(OpCode opcode, ExecutionContext context)
return;
}

byte[] script_hash = context.OpReader.ReadBytes(20);
byte[] script_hash = context.OpReader.SafeReadBytes(20);
if (script_hash.All(p => p == 0))
{
script_hash = context.EvaluationStack.Pop().GetByteArray();
Expand Down Expand Up @@ -987,7 +987,7 @@ private void ExecuteOp(OpCode opcode, ExecutionContext context)
if (opcode == OpCode.CALL_ED || opcode == OpCode.CALL_EDT)
script_hash = context.EvaluationStack.Pop().GetByteArray();
else
script_hash = context.OpReader.ReadBytes(20);
script_hash = context.OpReader.SafeReadBytes(20);
byte[] script = table.GetScript(script_hash);
if (script == null)
{
Expand Down
12 changes: 10 additions & 2 deletions src/neo-vm/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace Neo.VM
{
internal static class Helper
{
public static byte[] ReadVarBytes(this BinaryReader reader, int max = 0X7fffffc7)
public static byte[] ReadVarBytes(this BinaryReader reader, int max = 0x10000000)
{
return reader.ReadBytes((int)reader.ReadVarInt((ulong)max));
return reader.SafeReadBytes((int)reader.ReadVarInt((ulong)max));
}

public static ulong ReadVarInt(this BinaryReader reader, ulong max = ulong.MaxValue)
Expand All @@ -25,5 +25,13 @@ public static ulong ReadVarInt(this BinaryReader reader, ulong max = ulong.MaxVa
if (value > max) throw new FormatException();
return value;
}

public static byte[] SafeReadBytes(this BinaryReader reader, int count)
{
byte[] data = reader.ReadBytes(count);
if (data.Length < count)
throw new FormatException();
return data;
}
}
}