Skip to content

Commit

Permalink
Fix gosec issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby committed Feb 24, 2024
1 parent 8ba8a0d commit 4bd4f93
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 4 additions & 2 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,27 @@ import (
"unsafe"
)

// #nosec G103
// UnsafeString returns a string pointer without allocation
func UnsafeString(b []byte) string {
// the new way is slower `return unsafe.String(unsafe.SliceData(b), len(b))`
// unsafe.Pointer variant: 0.3538 ns/op vs unsafe.String variant: 0.5410 ns/op
// #nosec G103
return *(*string)(unsafe.Pointer(&b))
}

// #nosec G103
// UnsafeBytes returns a byte pointer without allocation.
func UnsafeBytes(s string) []byte {
// #nosec G103
return unsafe.Slice(unsafe.StringData(s), len(s))
}

// CopyString copies a string to make it immutable
func CopyString(s string) string {
// #nosec G103
return string(UnsafeBytes(s))
}

// #nosec G103
// CopyBytes copies a slice to make it immutable
func CopyBytes(b []byte) []byte {
tmp := make([]byte, len(b))
Expand Down
3 changes: 1 addition & 2 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func Walk(fs http.FileSystem, root string, walkFn filepath.WalkFunc) error {
return walk(fs, root, info, walkFn)
}

// #nosec G304
// ReadFile returns the raw content of a file
func ReadFile(path string, fs http.FileSystem) ([]byte, error) {
if fs != nil {
Expand All @@ -32,7 +31,7 @@ func ReadFile(path string, fs http.FileSystem) ([]byte, error) {
defer file.Close()
return io.ReadAll(file)
}
return os.ReadFile(path)
return os.ReadFile(path) // #nosec G304
}

// readDirNames reads the directory named by dirname and returns
Expand Down

0 comments on commit 4bd4f93

Please sign in to comment.