From 58af95a1d5e24950c0a1ab4389289ca70f594ba2 Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Thu, 26 Sep 2024 19:50:08 +0000 Subject: [PATCH] [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 --- go.mod | 2 +- go.sum | 4 +- internal/transport/delivery/worker_test.go | 6 +-- vendor/codeberg.org/gruf/go-byteutil/bytes.go | 42 +++---------------- .../codeberg.org/gruf/go-byteutil/reader.go | 36 ---------------- vendor/modules.txt | 4 +- 6 files changed, 13 insertions(+), 81 deletions(-) delete mode 100644 vendor/codeberg.org/gruf/go-byteutil/reader.go diff --git a/go.mod b/go.mod index a9e392ddb..d404415a0 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ replace modernc.org/sqlite => gitlab.com/NyaaaWhatsUpDoc/sqlite v1.29.9-concurre require ( codeberg.org/gruf/go-bytes v1.0.2 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-debug v1.3.0 codeberg.org/gruf/go-errors/v2 v2.3.2 diff --git a/go.sum b/go.sum index 8d9281d5d..92e1d964f 100644 --- a/go.sum +++ b/go.sum @@ -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-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-byteutil v1.2.0 h1:YoxkpUOoHS82BcPXfiIcWLe/YhS8QhpNUHdfuhN09QM= -codeberg.org/gruf/go-byteutil v1.2.0/go.mod h1:cWM3tgMCroSzqoBXUXMhvxTxYJp+TbCr6ioISRY5vSU= +codeberg.org/gruf/go-byteutil v1.3.0 h1:nRqJnCcRQ7xbfU6azw7zOzJrSMDIJHBqX6FL9vEMYmU= +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/go.mod h1:Thahfuf3PgHSv2+1zHpvhRdX97tx1WXurVNGWpZucAM= codeberg.org/gruf/go-debug v1.3.0 h1:PIRxQiWUFKtGOGZFdZ3Y0pqyfI0Xr87j224IYe2snZs= diff --git a/internal/transport/delivery/worker_test.go b/internal/transport/delivery/worker_test.go index 936ce6e1d..192ffcd37 100644 --- a/internal/transport/delivery/worker_test.go +++ b/internal/transport/delivery/worker_test.go @@ -18,6 +18,7 @@ package delivery_test import ( + "bytes" "fmt" "io" "math/rand" @@ -27,7 +28,6 @@ import ( "strings" "testing" - "codeberg.org/gruf/go-byteutil" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/httpclient" "github.com/superseriousbusiness/gotosocial/internal/queue" @@ -176,9 +176,9 @@ func requiresBody(method string) bool { func (t *testrequest) Generate(addr string) *http.Request { var body io.ReadCloser if t.body != nil { - var b byteutil.ReadNopCloser + var b bytes.Reader b.Reset(t.body) - body = &b + body = io.NopCloser(&b) } req, err := http.NewRequest(t.method, addr+t.uri, body) if err != nil { diff --git a/vendor/codeberg.org/gruf/go-byteutil/bytes.go b/vendor/codeberg.org/gruf/go-byteutil/bytes.go index 227feaaa7..880cb4b6d 100644 --- a/vendor/codeberg.org/gruf/go-byteutil/bytes.go +++ b/vendor/codeberg.org/gruf/go-byteutil/bytes.go @@ -1,7 +1,6 @@ package byteutil import ( - "reflect" "unsafe" ) @@ -16,44 +15,13 @@ func Copy(b []byte) []byte { } // B2S returns a string representation of []byte without allocation. -// -// According to the Go spec strings are immutable and byte slices are not. The way this gets implemented is strings under the hood are: -// -// 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 :) +// Since Go strings are immutable, the bytes passed to String must +// not be modified as long as the returned string value exists. 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). -// See B2S() code comment, and this function's implementation for a better understanding. +// S2B returns a []byte representation of string without allocation. func S2B(s string) []byte { - var b []byte - - // 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 + return unsafe.Slice(unsafe.StringData(s), len(s)) } diff --git a/vendor/codeberg.org/gruf/go-byteutil/reader.go b/vendor/codeberg.org/gruf/go-byteutil/reader.go deleted file mode 100644 index 94c755ff4..000000000 --- a/vendor/codeberg.org/gruf/go-byteutil/reader.go +++ /dev/null @@ -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 -} diff --git a/vendor/modules.txt b/vendor/modules.txt index 888895cad..aadcc4893 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -4,8 +4,8 @@ codeberg.org/gruf/go-bytes # codeberg.org/gruf/go-bytesize v1.0.3 ## explicit; go 1.17 codeberg.org/gruf/go-bytesize -# codeberg.org/gruf/go-byteutil v1.2.0 -## explicit; go 1.16 +# codeberg.org/gruf/go-byteutil v1.3.0 +## explicit; go 1.20 codeberg.org/gruf/go-byteutil # codeberg.org/gruf/go-cache/v3 v3.5.7 ## explicit; go 1.19