[chore] bump go-byteutil v1.2.0 -> v1.3.0 (#3356)

* bump go-byteutil v1.2.0 -> v1.3.0 which has safer (as in long-term API consistency) byte <-> string conversions

* fix test relying on byteutil exported type no longer existing
This commit is contained in:
kim 2024-09-26 19:50:08 +00:00 committed by GitHub
parent f3e2d36d64
commit 58af95a1d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 13 additions and 81 deletions

2
go.mod
View file

@ -7,7 +7,7 @@ replace modernc.org/sqlite => gitlab.com/NyaaaWhatsUpDoc/sqlite v1.29.9-concurre
require ( require (
codeberg.org/gruf/go-bytes v1.0.2 codeberg.org/gruf/go-bytes v1.0.2
codeberg.org/gruf/go-bytesize v1.0.3 codeberg.org/gruf/go-bytesize v1.0.3
codeberg.org/gruf/go-byteutil v1.2.0 codeberg.org/gruf/go-byteutil v1.3.0
codeberg.org/gruf/go-cache/v3 v3.5.7 codeberg.org/gruf/go-cache/v3 v3.5.7
codeberg.org/gruf/go-debug v1.3.0 codeberg.org/gruf/go-debug v1.3.0
codeberg.org/gruf/go-errors/v2 v2.3.2 codeberg.org/gruf/go-errors/v2 v2.3.2

4
go.sum
View file

@ -34,8 +34,8 @@ codeberg.org/gruf/go-bytes v1.0.2 h1:malqE42Ni+h1nnYWBUAJaDDtEzF4aeN4uPN8DfMNNvo
codeberg.org/gruf/go-bytes v1.0.2/go.mod h1:1v/ibfaosfXSZtRdW2rWaVrDXMc9E3bsi/M9Ekx39cg= codeberg.org/gruf/go-bytes v1.0.2/go.mod h1:1v/ibfaosfXSZtRdW2rWaVrDXMc9E3bsi/M9Ekx39cg=
codeberg.org/gruf/go-bytesize v1.0.3 h1:Tz8tCxhPLeyM5VryuBNjUHgKmLj4Bx9RbPaUSA3qg6g= codeberg.org/gruf/go-bytesize v1.0.3 h1:Tz8tCxhPLeyM5VryuBNjUHgKmLj4Bx9RbPaUSA3qg6g=
codeberg.org/gruf/go-bytesize v1.0.3/go.mod h1:n/GU8HzL9f3UNp/mUKyr1qVmTlj7+xacpp0OHfkvLPs= codeberg.org/gruf/go-bytesize v1.0.3/go.mod h1:n/GU8HzL9f3UNp/mUKyr1qVmTlj7+xacpp0OHfkvLPs=
codeberg.org/gruf/go-byteutil v1.2.0 h1:YoxkpUOoHS82BcPXfiIcWLe/YhS8QhpNUHdfuhN09QM= codeberg.org/gruf/go-byteutil v1.3.0 h1:nRqJnCcRQ7xbfU6azw7zOzJrSMDIJHBqX6FL9vEMYmU=
codeberg.org/gruf/go-byteutil v1.2.0/go.mod h1:cWM3tgMCroSzqoBXUXMhvxTxYJp+TbCr6ioISRY5vSU= codeberg.org/gruf/go-byteutil v1.3.0/go.mod h1:chgnZz1LUcfaObaIFglxF5MRYQkJGjQf4WwVz95ccCM=
codeberg.org/gruf/go-cache/v3 v3.5.7 h1:5hut49a8Wp3hdwrCEJYj6pHY2aRR1hyTmkK4+wHVYq4= codeberg.org/gruf/go-cache/v3 v3.5.7 h1:5hut49a8Wp3hdwrCEJYj6pHY2aRR1hyTmkK4+wHVYq4=
codeberg.org/gruf/go-cache/v3 v3.5.7/go.mod h1:Thahfuf3PgHSv2+1zHpvhRdX97tx1WXurVNGWpZucAM= codeberg.org/gruf/go-cache/v3 v3.5.7/go.mod h1:Thahfuf3PgHSv2+1zHpvhRdX97tx1WXurVNGWpZucAM=
codeberg.org/gruf/go-debug v1.3.0 h1:PIRxQiWUFKtGOGZFdZ3Y0pqyfI0Xr87j224IYe2snZs= codeberg.org/gruf/go-debug v1.3.0 h1:PIRxQiWUFKtGOGZFdZ3Y0pqyfI0Xr87j224IYe2snZs=

View file

@ -18,6 +18,7 @@
package delivery_test package delivery_test
import ( import (
"bytes"
"fmt" "fmt"
"io" "io"
"math/rand" "math/rand"
@ -27,7 +28,6 @@ import (
"strings" "strings"
"testing" "testing"
"codeberg.org/gruf/go-byteutil"
"github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/httpclient" "github.com/superseriousbusiness/gotosocial/internal/httpclient"
"github.com/superseriousbusiness/gotosocial/internal/queue" "github.com/superseriousbusiness/gotosocial/internal/queue"
@ -176,9 +176,9 @@ func requiresBody(method string) bool {
func (t *testrequest) Generate(addr string) *http.Request { func (t *testrequest) Generate(addr string) *http.Request {
var body io.ReadCloser var body io.ReadCloser
if t.body != nil { if t.body != nil {
var b byteutil.ReadNopCloser var b bytes.Reader
b.Reset(t.body) b.Reset(t.body)
body = &b body = io.NopCloser(&b)
} }
req, err := http.NewRequest(t.method, addr+t.uri, body) req, err := http.NewRequest(t.method, addr+t.uri, body)
if err != nil { if err != nil {

View file

@ -1,7 +1,6 @@
package byteutil package byteutil
import ( import (
"reflect"
"unsafe" "unsafe"
) )
@ -16,44 +15,13 @@ func Copy(b []byte) []byte {
} }
// B2S returns a string representation of []byte without allocation. // B2S returns a string representation of []byte without allocation.
// // Since Go strings are immutable, the bytes passed to String must
// According to the Go spec strings are immutable and byte slices are not. The way this gets implemented is strings under the hood are: // not be modified as long as the returned string value exists.
//
// type StringHeader struct {
// Data uintptr
// Len int
// }
//
// while slices are:
//
// type SliceHeader struct {
// Data uintptr
// Len int
// Cap int
// }
//
// because being mutable, you can change the data, length etc, but the string has to promise to be read-only to all who get copies of it.
//
// So in practice when you do a conversion of `string(byteSlice)` it actually performs an allocation because it has to copy the contents of the byte slice into a safe read-only state.
//
// Being that the shared fields are in the same struct indices (no different offsets), means that if you have a byte slice you can "forcibly" cast it to a string. Which in a lot of situations can be risky, because then it means you have a string that is NOT immutable, as if someone changes the data in the originating byte slice then the string will reflect that change! Now while this does seem hacky, and it _kind_ of is, it is something that you see performed in the standard library. If you look at the definition for `strings.Builder{}.String()` you'll see this :)
func B2S(b []byte) string { func B2S(b []byte) string {
return *(*string)(unsafe.Pointer(&b)) return unsafe.String(unsafe.SliceData(b), len(b))
} }
// S2B returns a []byte representation of string without allocation (minus slice header). // S2B returns a []byte representation of string without allocation.
// See B2S() code comment, and this function's implementation for a better understanding.
func S2B(s string) []byte { func S2B(s string) []byte {
var b []byte return unsafe.Slice(unsafe.StringData(s), len(s))
// Get byte + string headers
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
// Manually set bytes to string
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
return b
} }

View file

@ -1,36 +0,0 @@
package byteutil
import "bytes"
// Reader wraps a bytes.Reader{} to provide Rewind() capabilities.
type Reader struct {
B []byte
bytes.Reader
}
// NewReader returns a new Reader{} instance reset to b.
func NewReader(b []byte) *Reader {
r := &Reader{}
r.Reset(b)
return r
}
// Reset resets the Reader{} to be reading from b and sets Reader{}.B.
func (r *Reader) Reset(b []byte) {
r.B = b
r.Rewind()
}
// Rewind resets the Reader{} to be reading from the start of Reader{}.B.
func (r *Reader) Rewind() {
r.Reader.Reset(r.B)
}
// ReadNopCloser wraps a Reader{} to provide nop close method.
type ReadNopCloser struct {
Reader
}
func (*ReadNopCloser) Close() error {
return nil
}

4
vendor/modules.txt vendored
View file

@ -4,8 +4,8 @@ codeberg.org/gruf/go-bytes
# codeberg.org/gruf/go-bytesize v1.0.3 # codeberg.org/gruf/go-bytesize v1.0.3
## explicit; go 1.17 ## explicit; go 1.17
codeberg.org/gruf/go-bytesize codeberg.org/gruf/go-bytesize
# codeberg.org/gruf/go-byteutil v1.2.0 # codeberg.org/gruf/go-byteutil v1.3.0
## explicit; go 1.16 ## explicit; go 1.20
codeberg.org/gruf/go-byteutil codeberg.org/gruf/go-byteutil
# codeberg.org/gruf/go-cache/v3 v3.5.7 # codeberg.org/gruf/go-cache/v3 v3.5.7
## explicit; go 1.19 ## explicit; go 1.19