From ed1acafa4249f04f7df2f821a02cd1365f25b121 Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Sun, 12 Nov 2023 15:38:58 +0100 Subject: [PATCH 1/5] Fix Instruction error --- src/Neo.VM/Instruction.cs | 9 ++++++++- tests/Neo.VM.Tests/UtScript.cs | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Neo.VM/Instruction.cs b/src/Neo.VM/Instruction.cs index 5451df55..7b43c7c7 100644 --- a/src/Neo.VM/Instruction.cs +++ b/src/Neo.VM/Instruction.cs @@ -205,14 +205,21 @@ internal Instruction(ReadOnlyMemory 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}"); operandSize = span[ip]; break; case 2: + if (ip + 1 >= span.Length) + throw new BadScriptException($"Instrucion out of bounds. InstructionPointer: {ip}"); operandSize = BinaryPrimitives.ReadUInt16LittleEndian(span[ip..]); break; case 4: + if (ip + 3 >= span.Length) + throw new BadScriptException($"Instrucion out of bounds. InstructionPointer: {ip}"); 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}"); break; } ip += operandSizePrefix; diff --git a/tests/Neo.VM.Tests/UtScript.cs b/tests/Neo.VM.Tests/UtScript.cs index a303ca03..7e5c4014 100644 --- a/tests/Neo.VM.Tests/UtScript.cs +++ b/tests/Neo.VM.Tests/UtScript.cs @@ -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(() => new Script(rawScript, true)); + + rawScript = new byte[] { (byte)OpCode.PUSHDATA2 }; + Assert.ThrowsException(() => new Script(rawScript, true)); + + rawScript = new byte[] { (byte)OpCode.PUSHDATA4 }; + Assert.ThrowsException(() => new Script(rawScript, true)); } [TestMethod] From bee4c43bd833c179cceb096e03ac6004495b2b3b Mon Sep 17 00:00:00 2001 From: Jimmy Date: Mon, 13 Nov 2023 00:11:29 +0800 Subject: [PATCH 2/5] Update src/Neo.VM/Instruction.cs --- src/Neo.VM/Instruction.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Neo.VM/Instruction.cs b/src/Neo.VM/Instruction.cs index 7b43c7c7..83f84633 100644 --- a/src/Neo.VM/Instruction.cs +++ b/src/Neo.VM/Instruction.cs @@ -219,7 +219,7 @@ internal Instruction(ReadOnlyMemory script, int ip) : this((OpCode)script. throw new BadScriptException($"Instrucion out of bounds. InstructionPointer: {ip}"); operandSize = BinaryPrimitives.ReadInt32LittleEndian(span[ip..]); if (operandSize < 0) - throw new BadScriptException($"Instrucion out of bounds. InstructionPointer: {ip}, operandSize: {operandSize}"); + throw new BadScriptException($"Instruction out of bounds. InstructionPointer: {ip}, operandSize: {operandSize}"); break; } ip += operandSizePrefix; From 76f18ae62554afc06609b88fd04d9a8751ba8d43 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Mon, 13 Nov 2023 00:11:36 +0800 Subject: [PATCH 3/5] Update src/Neo.VM/Instruction.cs --- src/Neo.VM/Instruction.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Neo.VM/Instruction.cs b/src/Neo.VM/Instruction.cs index 83f84633..dad0e653 100644 --- a/src/Neo.VM/Instruction.cs +++ b/src/Neo.VM/Instruction.cs @@ -216,7 +216,7 @@ internal Instruction(ReadOnlyMemory script, int ip) : this((OpCode)script. break; case 4: if (ip + 3 >= span.Length) - throw new BadScriptException($"Instrucion out of bounds. InstructionPointer: {ip}"); + throw new BadScriptException($"Instruction out of bounds. InstructionPointer: {ip}"); operandSize = BinaryPrimitives.ReadInt32LittleEndian(span[ip..]); if (operandSize < 0) throw new BadScriptException($"Instruction out of bounds. InstructionPointer: {ip}, operandSize: {operandSize}"); From b24642dc127097150c5fd93809dfab774312aabe Mon Sep 17 00:00:00 2001 From: Jimmy Date: Mon, 13 Nov 2023 00:11:42 +0800 Subject: [PATCH 4/5] Update src/Neo.VM/Instruction.cs --- src/Neo.VM/Instruction.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Neo.VM/Instruction.cs b/src/Neo.VM/Instruction.cs index dad0e653..c98109cd 100644 --- a/src/Neo.VM/Instruction.cs +++ b/src/Neo.VM/Instruction.cs @@ -211,7 +211,7 @@ internal Instruction(ReadOnlyMemory script, int ip) : this((OpCode)script. break; case 2: if (ip + 1 >= span.Length) - throw new BadScriptException($"Instrucion out of bounds. InstructionPointer: {ip}"); + throw new BadScriptException($"Instruction out of bounds. InstructionPointer: {ip}"); operandSize = BinaryPrimitives.ReadUInt16LittleEndian(span[ip..]); break; case 4: From 76929c214b35eb018aea15ecebe157d6638cd841 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Mon, 13 Nov 2023 00:11:49 +0800 Subject: [PATCH 5/5] Update src/Neo.VM/Instruction.cs --- src/Neo.VM/Instruction.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Neo.VM/Instruction.cs b/src/Neo.VM/Instruction.cs index c98109cd..48b48c94 100644 --- a/src/Neo.VM/Instruction.cs +++ b/src/Neo.VM/Instruction.cs @@ -206,7 +206,7 @@ internal Instruction(ReadOnlyMemory script, int ip) : this((OpCode)script. break; case 1: if (ip >= span.Length) - throw new BadScriptException($"Instrucion out of bounds. InstructionPointer: {ip}"); + throw new BadScriptException($"Instruction out of bounds. InstructionPointer: {ip}"); operandSize = span[ip]; break; case 2: