From 77bacc255f0a693fa0d534e7e16e56bf95989c49 Mon Sep 17 00:00:00 2001 From: Vu Vo Date: Fri, 29 Oct 2021 17:16:41 +0700 Subject: [PATCH] fixbug substring3 opcode pop wrong order --- .../runtime/src/interpreter/opcode-list.ts | 18 +++++++++--------- packages/runtime/src/interpreter/opcode.ts | 4 ++-- .../test/src/interpreter/opcode-list.ts | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/runtime/src/interpreter/opcode-list.ts b/packages/runtime/src/interpreter/opcode-list.ts index 3beaab8f4..985d5532f 100644 --- a/packages/runtime/src/interpreter/opcode-list.ts +++ b/packages/runtime/src/interpreter/opcode-list.ts @@ -1152,19 +1152,19 @@ export class Substring extends Op { }; execute (stack: TEALStack): void { - const byteString = this.assertBytes(stack.pop(), this.line); - const start = this.assertUint8(this.start, this.line); const end = this.assertUint8(this.end, this.line); + const start = this.assertUint8(this.start, this.line); + const byteString = this.assertBytes(stack.pop(), this.line); - const subString = this.subString(start, end, byteString, this.line); + const subString = this.subString(byteString, start, end, this.line); stack.push(subString); } } -// pop last byte string A and two integers B and C. -// Extract last range of bytes from A starting at B up to -// but not including C, push the substring result. If C < B, -// or either is larger than the string length, the program fails +// pop a byte-array A and two integers B and C. +// Extract a range of bytes from A starting at B up to but not including C, +// push the substring result. If C < B, or either is larger than the array length, +// the program fails // push to stack [...stack, substring] export class Substring3 extends Op { readonly line: number; @@ -1180,11 +1180,11 @@ export class Substring3 extends Op { }; execute (stack: TEALStack): void { - const byteString = this.assertBytes(stack.pop(), this.line); const end = this.assertBigInt(stack.pop(), this.line); const start = this.assertBigInt(stack.pop(), this.line); + const byteString = this.assertBytes(stack.pop(), this.line); - const subString = this.subString(start, end, byteString, this.line); + const subString = this.subString(byteString, start, end, this.line); stack.push(subString); } } diff --git a/packages/runtime/src/interpreter/opcode.ts b/packages/runtime/src/interpreter/opcode.ts index 0d3aab24e..db8daa076 100644 --- a/packages/runtime/src/interpreter/opcode.ts +++ b/packages/runtime/src/interpreter/opcode.ts @@ -172,12 +172,12 @@ export class Op { /** * Returns substring from given string (if it exists) + * @param byteString given string as bytes * @param start starting index * @param end ending index - * @param byteString given string as bytes * @param line line number in TEAL file */ - subString (start: bigint, end: bigint, byteString: Uint8Array, line: number): Uint8Array { + subString (byteString: Uint8Array, start: bigint, end: bigint, line: number): Uint8Array { if (end < start) { throw new RuntimeError(RUNTIME_ERRORS.TEAL.SUBSTRING_END_BEFORE_START, { line: line }); } diff --git a/packages/runtime/test/src/interpreter/opcode-list.ts b/packages/runtime/test/src/interpreter/opcode-list.ts index b805b2ce2..9129e7b15 100644 --- a/packages/runtime/test/src/interpreter/opcode-list.ts +++ b/packages/runtime/test/src/interpreter/opcode-list.ts @@ -1493,9 +1493,9 @@ describe("Teal Opcodes", function () { const stack = new Stack(); it("should return correct substring", function () { + stack.push(parsing.stringToBytes("Algorand")); stack.push(0n); stack.push(4n); - stack.push(parsing.stringToBytes("Algorand")); const op = new Substring3([], 1); op.execute(stack); @@ -1518,7 +1518,7 @@ describe("Teal Opcodes", function () { const start = end + 1n; execExpectError( stack, - [start, end, parsing.stringToBytes("Algorand")], + [parsing.stringToBytes("Algorand"), start, end], new Substring3([], 1), RUNTIME_ERRORS.TEAL.SUBSTRING_END_BEFORE_START ); @@ -1527,7 +1527,7 @@ describe("Teal Opcodes", function () { it("should throw error because range beyong string", execExpectError( stack, - [0n, 40n, parsing.stringToBytes("Algorand")], + [parsing.stringToBytes("Algorand"), 0n, 40n], new Substring3([], 1), RUNTIME_ERRORS.TEAL.SUBSTRING_RANGE_BEYOND )