Skip to content

Commit

Permalink
add IndexByteString to syscall package to impede use of internal/bytealg
Browse files Browse the repository at this point in the history
Signed-off-by: leongross <leon.gross@9elements.com>
  • Loading branch information
leongross committed Jul 16, 2024
1 parent acee51a commit 33f688d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
38 changes: 38 additions & 0 deletions src/syscall/str.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package syscall

import "errors"

// clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte.
func clen(n []byte) int {
for i := 0; i < len(n); i++ {
Expand All @@ -13,3 +15,39 @@ func clen(n []byte) int {
}
return len(n)
}

// BytePtrFromString returns a pointer to a NUL-terminated array of
// bytes containing the text of s. If s contains a NUL byte at any
// location, it returns (nil, [EINVAL]).
//
// For darwin || nintendoswitch || wasi || wasip1 this is already implemented in /src/syscall/syscall_libc.go:266:6
// func BytePtrFromString(s string) (*byte, error) {
// a, err := ByteSliceFromString(s)
// if err != nil {
// return nil, err
// }
// return &a[0], nil
// }

// copied from upstream src/internal/bytealg/indexbyte_generic.go since we cannot use the internal bytealg package
func IndexByteString(s string, c byte) int {
for i := 0; i < len(s); i++ {
if s[i] == c {
return i
}
}
return -1
}

// ByteSliceFromString returns a NUL-terminated slice of bytes
// containing the text of s. If s contains a NUL byte at any
// location, it returns (nil, [EINVAL]).
// https://cs.opensource.google/go/go/+/master:src/syscall/syscall.go;l=45;drc=94982a07825aec711f11c97283e99e467838d616
func ByteSliceFromString(s string) ([]byte, error) {
if IndexByteString(s, 0) != -1 {
return nil, errors.New("contains NUL")
}
a := make([]byte, len(s)+1)
copy(a, s)
return a, nil
}
31 changes: 0 additions & 31 deletions src/syscall/syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package syscall
import (
"errors"
"sync/atomic"

"internal/bytealg"
)

const (
Expand All @@ -13,11 +11,6 @@ const (
AF_INET6 = 0xa
)

const (
// #define EINVAL 22 /* Invalid argument */
EINVAL = 22
)

func Exit(code int)

type Rlimit struct {
Expand All @@ -33,27 +26,3 @@ var origRlimitNofile atomic.Value // of Rlimit
func Setrlimit(resource int, rlim *Rlimit) error {
return errors.New("Setrlimit not implemented")
}

// ByteSliceFromString returns a NUL-terminated slice of bytes
// containing the text of s. If s contains a NUL byte at any
// location, it returns (nil, [EINVAL]).
// https://cs.opensource.google/go/go/+/master:src/syscall/syscall.go;l=45;drc=94982a07825aec711f11c97283e99e467838d616
func ByteSliceFromString(s string) ([]byte, error) {
if bytealg.IndexByteString(s, 0) != -1 {
return nil, errors.New("contains NUL")
}
a := make([]byte, len(s)+1)
copy(a, s)
return a, nil
}

// BytePtrFromString returns a pointer to a NUL-terminated array of
// bytes containing the text of s. If s contains a NUL byte at any
// location, it returns (nil, [EINVAL]).
func BytePtrFromString(s string) (*byte, error) {
a, err := ByteSliceFromString(s)
if err != nil {
return nil, err
}
return &a[0], nil
}

0 comments on commit 33f688d

Please sign in to comment.