mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2026-05-17 08:36:55 +03:00
lib/mergeset: misuse of unsafe.Pointer
It appears the `lib/mergeset.Item` violates the 3rd rule https://pkg.go.dev/unsafe#Pointer > (3) Conversion of a Pointer to a uintptr and back, with arithmetic. > > Unlike in C, it is not valid to advance a pointer just beyond the end of its original allocation: > ``` > // INVALID: end points outside allocated space. > b := make([]byte, n)` > end = unsafe.Pointer(uintptr(unsafe.Pointer(&b[0])) + uintptr(n)) > ``` `CGO_ENABLED=0 go test -trimpath -gcflags=all=-d=checkptr -run TestTableAddItemsSerial` ``` fatal error: checkptr: pointer arithmetic result points to invalid allocation This commit adds a special path to item encoding, that prevents invalid memory references. See this PR for details: https://github.com/VictoriaMetrics/VictoriaMetrics/pull/9264
This commit is contained in:
@@ -28,16 +28,22 @@ type Item struct {
|
||||
//
|
||||
// The returned bytes representation belongs to data.
|
||||
func (it Item) Bytes(data []byte) []byte {
|
||||
n := int(it.End - it.Start)
|
||||
return unsafe.Slice((*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(data)))+uintptr(it.Start))), n)
|
||||
n := it.End - it.Start
|
||||
if n == 0 {
|
||||
return nil
|
||||
}
|
||||
return unsafe.Slice((*byte)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(data)), it.Start)), n)
|
||||
}
|
||||
|
||||
// String returns string representation of it obtained from data.
|
||||
//
|
||||
// The returned string representation belongs to data.
|
||||
func (it Item) String(data []byte) string {
|
||||
n := int(it.End - it.Start)
|
||||
return unsafe.String((*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(data)))+uintptr(it.Start))), n)
|
||||
n := it.End - it.Start
|
||||
if n == 0 {
|
||||
return ""
|
||||
}
|
||||
return unsafe.String((*byte)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(data)), it.Start)), n)
|
||||
}
|
||||
|
||||
func (ib *inmemoryBlock) Len() int {
|
||||
|
||||
Reference in New Issue
Block a user