Skip to content

Commit 044812d

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

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
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: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package syscall
33
import (
44
"errors"
55
"sync/atomic"
6-
7-
"internal/bytealg"
86
)
97

108
const (
@@ -13,11 +11,6 @@ const (
1311
AF_INET6 = 0xa
1412
)
1513

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

2316
type Rlimit struct {
@@ -39,7 +32,7 @@ func Setrlimit(resource int, rlim *Rlimit) error {
3932
// location, it returns (nil, [EINVAL]).
4033
// https://cs.opensource.google/go/go/+/master:src/syscall/syscall.go;l=45;drc=94982a07825aec711f11c97283e99e467838d616
4134
func ByteSliceFromString(s string) ([]byte, error) {
42-
if bytealg.IndexByteString(s, 0) != -1 {
35+
if IndexByteString(s, 0) != -1 {
4336
return nil, errors.New("contains NUL")
4437
}
4538
a := make([]byte, len(s)+1)
@@ -50,10 +43,22 @@ func ByteSliceFromString(s string) ([]byte, error) {
5043
// BytePtrFromString returns a pointer to a NUL-terminated array of
5144
// bytes containing the text of s. If s contains a NUL byte at any
5245
// location, it returns (nil, [EINVAL]).
46+
//
47+
// For darwin || nintendoswitch || wasi || wasip1 this is already implemented in /src/syscall/syscall_libc.go:266:6
5348
func BytePtrFromString(s string) (*byte, error) {
5449
a, err := ByteSliceFromString(s)
5550
if err != nil {
5651
return nil, err
5752
}
5853
return &a[0], nil
5954
}
55+
56+
// copied from upstream src/internal/bytealg/indexbyte_generic.go since we cannot use the internal bytealg package
57+
func IndexByteString(s string, c byte) int {
58+
for i := 0; i < len(s); i++ {
59+
if s[i] == c {
60+
return i
61+
}
62+
}
63+
return -1
64+
}

0 commit comments

Comments
 (0)