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 2 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
15 changes: 8 additions & 7 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 All @@ -189,7 +189,8 @@ private void ExecuteOp(OpCode opcode, ExecutionContext context)
}
break;
case OpCode.SYSCALL:
if (Service?.Invoke(context.OpReader.ReadVarBytes(252), this) != true)
int length = context.OpReader.ReadByte();
if ((length > 252) || (Service?.Invoke(context.OpReader.SafeReadBytes(length), this) != true))
State |= VMState.FAULT;
break;

Expand Down Expand Up @@ -987,7 +988,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
22 changes: 4 additions & 18 deletions src/neo-vm/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,11 @@ namespace Neo.VM
{
internal static class Helper
{
public static byte[] ReadVarBytes(this BinaryReader reader, int max = 0X7fffffc7)
public static byte[] SafeReadBytes(this BinaryReader reader, int max = 0x1000000)
{
return reader.ReadBytes((int)reader.ReadVarInt((ulong)max));
}

public static ulong ReadVarInt(this BinaryReader reader, ulong max = ulong.MaxValue)
{
byte fb = reader.ReadByte();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't remove this logic

ulong value;
if (fb == 0xFD)
value = reader.ReadUInt16();
else if (fb == 0xFE)
value = reader.ReadUInt32();
else if (fb == 0xFF)
value = reader.ReadUInt64();
else
value = fb;
if (value > max) throw new FormatException();
return value;
if((max > 0x1000000) || (!reader.BaseStream.CanSeek) || (reader.BaseStream.Length - reader.BaseStream.Position) < max))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to throw an exception, then is not needed to check CanSeek, because this could throw an exception when you access to length in wrong streams

throw new FormatException;
return reader.ReadBytes(max);
}
}
}