Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug substring3 opcode pop wrong order #505

Merged
merged 1 commit into from
Oct 29, 2021
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
18 changes: 9 additions & 9 deletions packages/runtime/src/interpreter/opcode-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/src/interpreter/opcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/test/src/interpreter/opcode-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1493,9 +1493,9 @@ describe("Teal Opcodes", function () {
const stack = new Stack<StackElem>();

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);
Expand All @@ -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
);
Expand All @@ -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
)
Expand Down