Add some unsafe helper to track errors
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const maxInt = uintptr(int(^uint(0) >> 1))
|
||||||
|
|
||||||
|
|
||||||
|
func UnsafeSubsliceOffset(data []byte, subslice []byte) int {
|
||||||
|
datap := (*reflect.SliceHeader)(unsafe.Pointer(&data))
|
||||||
|
hlp := (*reflect.SliceHeader)(unsafe.Pointer(&subslice))
|
||||||
|
|
||||||
|
|
||||||
|
if hlp.Data < datap.Data {
|
||||||
|
panic(fmt.Errorf("subslice address (%d) is before data address (%d)", hlp.Data, datap.Data))
|
||||||
|
}
|
||||||
|
offset := hlp.Data - datap.Data
|
||||||
|
|
||||||
|
if offset > maxInt {
|
||||||
|
panic(fmt.Errorf("slice offset larger than int (%d)", offset))
|
||||||
|
}
|
||||||
|
|
||||||
|
intoffset := int(offset)
|
||||||
|
|
||||||
|
if intoffset >= datap.Len {
|
||||||
|
panic(fmt.Errorf("slice offset (%d) is farther than data length (%d)", intoffset, datap.Len))
|
||||||
|
}
|
||||||
|
|
||||||
|
if intoffset + hlp.Len > datap.Len {
|
||||||
|
panic(fmt.Errorf("slice ends (%d+%d) is farther than data length (%d)", intoffset, hlp.Len, datap.Len))
|
||||||
|
}
|
||||||
|
|
||||||
|
return intoffset
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package errors_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/pelletier/go-toml/v2/internal/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUnsafeSubsliceOffsetValid(t *testing.T) {
|
||||||
|
examples := []struct{
|
||||||
|
desc string
|
||||||
|
test func() ([]byte, []byte)
|
||||||
|
offset int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "simple",
|
||||||
|
test: func() ([]byte, []byte) {
|
||||||
|
data := []byte("hello")
|
||||||
|
return data, data[1:]
|
||||||
|
},
|
||||||
|
offset: 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, e := range examples {
|
||||||
|
t.Run(e.desc, func(t *testing.T) {
|
||||||
|
d, s := e.test()
|
||||||
|
offset := errors.UnsafeSubsliceOffset(d, s)
|
||||||
|
assert.Equal(t, e.offset, offset)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnsafeSubsliceOffsetInvalid(t *testing.T) {
|
||||||
|
examples := []struct{
|
||||||
|
desc string
|
||||||
|
test func() ([]byte, []byte)
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "unrelated arrays",
|
||||||
|
test: func() ([]byte, []byte) {
|
||||||
|
return []byte("one"), []byte("two")
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "slice starts before data",
|
||||||
|
test: func() ([]byte, []byte) {
|
||||||
|
full := []byte("hello world")
|
||||||
|
return full[5:], full[1:]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "slice starts after data",
|
||||||
|
test: func() ([]byte, []byte) {
|
||||||
|
full := []byte("hello world")
|
||||||
|
return full[:3], full[5:]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "slice ends after data",
|
||||||
|
test: func() ([]byte, []byte) {
|
||||||
|
full := []byte("hello world")
|
||||||
|
return full[:5], full[3:8]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, e := range examples {
|
||||||
|
t.Run(e.desc, func(t *testing.T) {
|
||||||
|
d, s := e.test()
|
||||||
|
require.Panics(t, func() {
|
||||||
|
errors.UnsafeSubsliceOffset(d, s)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user