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

[CRE-42] Fix partial or truncated writes #989

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 5 additions & 3 deletions pkg/workflows/wasm/host/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,11 +728,13 @@ func write(memory, src []byte, ptr, size int32) int64 {
return -1
}

if len(src) != int(size) {
return -1
}

if int32(len(memory)) < ptr+size {
return -1
}
buffer := memory[ptr : ptr+size]
dataLen := int64(len(src))
copy(buffer, src)
return dataLen
return int64(copy(buffer, src))
}
26 changes: 20 additions & 6 deletions pkg/workflows/wasm/host/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,29 +553,43 @@ func Test_read(t *testing.T) {
}

func Test_write(t *testing.T) {
t.Run("successfully write to slice", func(t *testing.T) {
t.Run("OK-successfully_write_to_slice", func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these OK/NOK are out of character with the rest of the code base; recommend removing them

and, there is no need to explicitly underscore the names; the go tooling does it under the hood

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed 👍🏼

giveSrc := []byte("hello, world")
memory := make([]byte, 12)
n := write(memory, giveSrc, 0, int32(len(giveSrc)))
assert.Equal(t, n, int64(len(giveSrc)))
assert.Equal(t, []byte("hello, world"), memory[:len(giveSrc)])
})

t.Run("cannot write to slice because memory too small", func(t *testing.T) {
t.Run("NOK-cannot_write_to_slice_because_memory_too_small", func(t *testing.T) {
giveSrc := []byte("hello, world")
memory := make([]byte, len(giveSrc)-1)
n := write(memory, giveSrc, 0, int32(len(giveSrc)))
assert.Equal(t, n, int64(-1))
assert.Equal(t, int64(-1), n)
})

t.Run("fails to write to invalid access", func(t *testing.T) {
t.Run("NOK-fails_to_write_to_invalid_access", func(t *testing.T) {
giveSrc := []byte("hello, world")
memory := make([]byte, len(giveSrc))
n := write(memory, giveSrc, 0, -1)
assert.Equal(t, n, int64(-1))
assert.Equal(t, int64(-1), n)

n = write(memory, giveSrc, -1, 1)
assert.Equal(t, n, int64(-1))
assert.Equal(t, int64(-1), n)
})

t.Run("NOK-truncated_write_due_to_size_being_smaller_than_len", func(t *testing.T) {
giveSrc := []byte("hello, world")
memory := make([]byte, 12)
n := write(memory, giveSrc, 0, int32(len(giveSrc)-2))
assert.Equal(t, int64(-1), n)
})

t.Run("NOK-unwanted_data_when_size_exceeds_written_data", func(t *testing.T) {
giveSrc := []byte("hello, world")
memory := make([]byte, 20)
n := write(memory, giveSrc, 0, 20)
assert.Equal(t, int64(-1), n)
})
}

Expand Down
Loading