diff --git a/executor/executorMemory.go b/executor/executorMemory.go index 56bebe052..79a910e02 100644 --- a/executor/executorMemory.go +++ b/executor/executorMemory.go @@ -50,10 +50,12 @@ func MemLoadFromMemory(memory Memory, memPtr MemPtr, length MemLength) ([]byte, return nil, fmt.Errorf("mem load: %w", ErrMemoryNegativeLength) } - result := make([]byte, length) + var result []byte if isRequestedEndTooLarge { + result = make([]byte, memoryLength-uint32(memPtr)) copy(result, memoryView[memPtr:]) } else { + result = make([]byte, requestedEnd-memPtr) copy(result, memoryView[memPtr:requestedEnd]) } diff --git a/vmhost/contexts/runtime_test.go b/vmhost/contexts/runtime_test.go index afb0be054..becf2245b 100644 --- a/vmhost/contexts/runtime_test.go +++ b/vmhost/contexts/runtime_test.go @@ -653,7 +653,7 @@ func TestRuntimeContext_MemLoadCases(t *testing.T) { length = 9 memContents, err = memLoad(runtimeCtx, offset, length) require.Nil(t, err) - require.Equal(t, []byte{'e', 's', 't', ' ', 'd', 'a', 't', 'a', 0}, memContents) + require.Equal(t, []byte{'e', 's', 't', ' ', 'd', 'a', 't', 'a'}, memContents) // Zero length offset = int32(runtimeCtx.iTracker.instance.MemLength() - 8)