diff options
Diffstat (limited to 'libgo/go/syscall/syscall.go')
-rw-r--r-- | libgo/go/syscall/syscall.go | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/libgo/go/syscall/syscall.go b/libgo/go/syscall/syscall.go index 4efaaec..3090a5e 100644 --- a/libgo/go/syscall/syscall.go +++ b/libgo/go/syscall/syscall.go @@ -16,18 +16,47 @@ package syscall import "unsafe" -// StringByteSlice returns a NUL-terminated slice of bytes -// containing the text of s. +// StringByteSlice returns a NUL-terminated slice of bytes containing the text of s. +// If s contains a NUL byte this function panics instead of +// returning an error. func StringByteSlice(s string) []byte { + a, err := byteSliceFromString(s) + if err != nil { + panic("syscall: string with NUL passed to StringByteSlice") + } + return a +} + +// 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). +func byteSliceFromString(s string) ([]byte, error) { + for i := 0; i < len(s); i++ { + if s[i] == 0 { + return nil, EINVAL + } + } a := make([]byte, len(s)+1) copy(a, s) - return a + return a, nil } -// StringBytePtr returns a pointer to a NUL-terminated array of bytes -// containing the text of s. +// StringBytePtr returns a pointer to a NUL-terminated array of bytes containing the text of s. +// If s contains a NUL byte this function panics instead of +// returning an error. func StringBytePtr(s string) *byte { return &StringByteSlice(s)[0] } +// 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 +} + // Single-word zero for use when we need a valid pointer to 0 bytes. // See mksyscall.pl. var _zero uintptr |