Skip to content

Commit 33f688d

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

File tree

2 files changed

+38
-31
lines changed

2 files changed

+38
-31
lines changed

src/syscall/str.go

+38
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

-31
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 {
@@ -33,27 +26,3 @@ var origRlimitNofile atomic.Value // of Rlimit
3326
func Setrlimit(resource int, rlim *Rlimit) error {
3427
return errors.New("Setrlimit not implemented")
3528
}
36-
37-
// ByteSliceFromString returns a NUL-terminated slice of bytes
38-
// containing the text of s. If s contains a NUL byte at any
39-
// location, it returns (nil, [EINVAL]).
40-
// https://cs.opensource.google/go/go/+/master:src/syscall/syscall.go;l=45;drc=94982a07825aec711f11c97283e99e467838d616
41-
func ByteSliceFromString(s string) ([]byte, error) {
42-
if bytealg.IndexByteString(s, 0) != -1 {
43-
return nil, errors.New("contains NUL")
44-
}
45-
a := make([]byte, len(s)+1)
46-
copy(a, s)
47-
return a, nil
48-
}
49-
50-
// BytePtrFromString returns a pointer to a NUL-terminated array of
51-
// bytes containing the text of s. If s contains a NUL byte at any
52-
// location, it returns (nil, [EINVAL]).
53-
func BytePtrFromString(s string) (*byte, error) {
54-
a, err := ByteSliceFromString(s)
55-
if err != nil {
56-
return nil, err
57-
}
58-
return &a[0], nil
59-
}

0 commit comments

Comments
 (0)