From 64d1f6303f10c9221304631107d95405c8c5e375 Mon Sep 17 00:00:00 2001 From: Reynold Morel Date: Fri, 24 May 2024 15:34:26 -0400 Subject: [PATCH] Update getCode so it does not make a copy of the array --- rskj-core/src/main/java/org/ethereum/vm/VM.java | 11 ++++++----- .../main/java/org/ethereum/vm/program/Program.java | 6 +++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/rskj-core/src/main/java/org/ethereum/vm/VM.java b/rskj-core/src/main/java/org/ethereum/vm/VM.java index 2365d387028..f7568f1f067 100644 --- a/rskj-core/src/main/java/org/ethereum/vm/VM.java +++ b/rskj-core/src/main/java/org/ethereum/vm/VM.java @@ -775,7 +775,7 @@ protected void doCODESIZE() { // EXECUTION PHASE DataWord codeLength; if (op == OpCode.CODESIZE) { - codeLength = DataWord.valueOf(program.getCode().length); // during initialization it will return the initialization code size + codeLength = DataWord.valueOf(program.getCodeLength()); // during initialization it will return the initialization code size } else { DataWord address = program.stackPop(); codeLength = DataWord.valueOf(program.getCodeLengthAt(address)); @@ -879,13 +879,14 @@ protected void doCODECOPY() { int lengthData = lengthDataDW.intValueSafe(); // amount of bytes to copy int sizeToBeCopied; - if ((long) codeOffset + lengthData > fullCode.length) { + int fullCodeLength = fullCode.length; + if ((long) codeOffset + lengthData > fullCodeLength) { // if user wants to read more info from code what actual code has then.. // if all code that users wants lies after code has ended.. - if (codeOffset >=fullCode.length) { + if (codeOffset >= fullCodeLength) { sizeToBeCopied=0; // do not copy anything } else { - sizeToBeCopied = fullCode.length - codeOffset; // copy only the remaining + sizeToBeCopied = fullCodeLength - codeOffset; // copy only the remaining } } else @@ -898,7 +899,7 @@ protected void doCODECOPY() { // enough space to contain filling also. byte[] codeCopy = new byte[lengthData]; - if (codeOffset < fullCode.length) { + if (codeOffset < fullCodeLength) { System.arraycopy(fullCode, codeOffset, codeCopy, 0, sizeToBeCopied); } diff --git a/rskj-core/src/main/java/org/ethereum/vm/program/Program.java b/rskj-core/src/main/java/org/ethereum/vm/program/Program.java index 5a799524d32..1527e5e89b8 100644 --- a/rskj-core/src/main/java/org/ethereum/vm/program/Program.java +++ b/rskj-core/src/main/java/org/ethereum/vm/program/Program.java @@ -957,7 +957,11 @@ private RskAddress getOwnerRskAddress() { } public byte[] getCode() { - return Arrays.copyOf(ops, ops.length); + return ops; + } + + public int getCodeLength() { + return ops.length; } public Keccak256 getCodeHashAt(RskAddress addr, boolean standard) {