Skip to content

Uses unsafe.String where possible #2282

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

Merged
merged 1 commit into from
Jul 9, 2024
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
8 changes: 1 addition & 7 deletions imports/wasi_snapshot_preview1/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1962,17 +1962,11 @@ func pathSymlinkFn(_ context.Context, mod api.Module, params []uint64) experimen
return dir.FS.Symlink(
// Do not join old path since it's only resolved when dereference the link created here.
// And the dereference result depends on the opening directory's file descriptor at that point.
bufToStr(oldPathBuf),
unsafe.String(&oldPathBuf[0], int(oldPathLen)),
newPathName,
)
}

// bufToStr converts the given byte slice as string unsafely.
func bufToStr(buf []byte) string {
// TODO: use unsafe.String after flooring Go 1.20.
return *(*string)(unsafe.Pointer(&buf))
}

// pathUnlinkFile is the WASI function named PathUnlinkFileName which unlinks a
// file.
//
Expand Down
3 changes: 1 addition & 2 deletions internal/wasm/binary/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func decodeUTF8(r *bytes.Reader, contextFormat string, contextArgs ...interface{
return "", 0, fmt.Errorf("%s is not valid UTF-8", fmt.Sprintf(contextFormat, contextArgs...))
}

// TODO: use unsafe.String after flooring Go 1.20.
ret := *(*string)(unsafe.Pointer(&buf))
ret := unsafe.String(&buf[0], int(size))
return ret, size + uint32(sizeOfSize), nil
}
Loading