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

Fix Instruction error #518

Merged
merged 5 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion src/Neo.VM/Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,21 @@ internal Instruction(ReadOnlyMemory<byte> script, int ip) : this((OpCode)script.
operandSize = OperandSizeTable[(int)OpCode];
break;
case 1:
if (ip >= span.Length)
throw new BadScriptException($"Instrucion out of bounds. InstructionPointer: {ip}");
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
operandSize = span[ip];
break;
case 2:
if (ip + 1 >= span.Length)
throw new BadScriptException($"Instrucion out of bounds. InstructionPointer: {ip}");
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
operandSize = BinaryPrimitives.ReadUInt16LittleEndian(span[ip..]);
break;
case 4:
if (ip + 3 >= span.Length)
throw new BadScriptException($"Instrucion out of bounds. InstructionPointer: {ip}");
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
operandSize = BinaryPrimitives.ReadInt32LittleEndian(span[ip..]);
if (operandSize < 0) throw new BadScriptException();
if (operandSize < 0)
throw new BadScriptException($"Instrucion out of bounds. InstructionPointer: {ip}, operandSize: {operandSize}");
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
break;
}
ip += operandSizePrefix;
Expand Down
9 changes: 9 additions & 0 deletions tests/Neo.VM.Tests/UtScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public void StrictMode()

var script = new Script(rawScript, false);
Assert.AreEqual(2, script.Length);

rawScript = new byte[] { (byte)OpCode.PUSHDATA1 };
Assert.ThrowsException<BadScriptException>(() => new Script(rawScript, true));

rawScript = new byte[] { (byte)OpCode.PUSHDATA2 };
Assert.ThrowsException<BadScriptException>(() => new Script(rawScript, true));

rawScript = new byte[] { (byte)OpCode.PUSHDATA4 };
Assert.ThrowsException<BadScriptException>(() => new Script(rawScript, true));
}

[TestMethod]
Expand Down