Skip to content

Commit 925256e

Browse files
committed
add IndexByteString to syscall package to impede use of internal/bytealg
Signed-off-by: leongross <[email protected]>
1 parent a688dec commit 925256e

File tree

2 files changed

+38
-31
lines changed

2 files changed

+38
-31
lines changed

src/syscall/str.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package syscall
66

7+
import "errors"
8+
79
// clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte.
810
func clen(n []byte) int {
911
for i := 0; i < len(n); i++ {
@@ -13,3 +15,39 @@ func clen(n []byte) int {
1315
}
1416
return len(n)
1517
}
18+
19+
// BytePtrFromString returns a pointer to a NUL-terminated array of
20+
// bytes containing the text of s. If s contains a NUL byte at any
21+
// location, it returns (nil, [EINVAL]).
22+
//
23+
// For darwin || nintendoswitch || wasi || wasip1 this is already implemented in /src/syscall/syscall_libc.go:266:6
24+
// func BytePtrFromString(s string) (*byte, error) {
25+
// a, err := ByteSliceFromString(s)
26+
// if err != nil {
27+
// return nil, err
28+
// }
29+
// return &a[0], nil
30+
// }
31+
32+
// copied from upstream src/internal/bytealg/indexbyte_generic.go since we cannot use the internal bytealg package
33+
func IndexByteString(s string, c byte) int {
34+
for i := 0; i < len(s); i++ {
35+
if s[i] == c {
36+
return i
37+
}
38+
}
39+
return -1
40+
}
41+
42+
// ByteSliceFromString returns a NUL-terminated slice of bytes
43+
// containing the text of s. If s contains a NUL byte at any
44+
// location, it returns (nil, [EINVAL]).
45+
// https://cs.opensource.google/go/go/+/master:src/syscall/syscall.go;l=45;drc=94982a07825aec711f11c97283e99e467838d616
46+
func ByteSliceFromString(s string) ([]byte, error) {
47+
if IndexByteString(s, 0) != -1 {
48+
return nil, errors.New("contains NUL")
49+
}
50+
a := make([]byte, len(s)+1)
51+
copy(a, s)
52+
return a, nil
53+
}

src/syscall/syscall.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package syscall
22

33
import (
44
"errors"
5-
6-
"internal/bytealg"
75
)
86

97
const (
@@ -12,11 +10,6 @@ const (
1210
AF_INET6 = 0xa
1311
)
1412

15-
const (
16-
// #define EINVAL 22 /* Invalid argument */
17-
EINVAL = 22
18-
)
19-
2013
func Exit(code int)
2114

2215
type Rlimit struct {
@@ -27,27 +20,3 @@ type Rlimit struct {
2720
func Setrlimit(resource int, rlim *Rlimit) error {
2821
return errors.New("Setrlimit not implemented")
2922
}
30-
31-
// ByteSliceFromString returns a NUL-terminated slice of bytes
32-
// containing the text of s. If s contains a NUL byte at any
33-
// location, it returns (nil, [EINVAL]).
34-
// https://cs.opensource.google/go/go/+/master:src/syscall/syscall.go;l=45;drc=94982a07825aec711f11c97283e99e467838d616
35-
func ByteSliceFromString(s string) ([]byte, error) {
36-
if bytealg.IndexByteString(s, 0) != -1 {
37-
return nil, errors.New("contains NUL")
38-
}
39-
a := make([]byte, len(s)+1)
40-
copy(a, s)
41-
return a, nil
42-
}
43-
44-
// BytePtrFromString returns a pointer to a NUL-terminated array of
45-
// bytes containing the text of s. If s contains a NUL byte at any
46-
// location, it returns (nil, [EINVAL]).
47-
func BytePtrFromString(s string) (*byte, error) {
48-
a, err := ByteSliceFromString(s)
49-
if err != nil {
50-
return nil, err
51-
}
52-
return &a[0], nil
53-
}

0 commit comments

Comments
 (0)