Skip to content

Commit

Permalink
pkg/devicemapper: fix invalid usage of reflect.SliceHeader
Browse files Browse the repository at this point in the history
The current usage of reflect.SliceHeader violates rule 6th of
unsafe.Pointer conversion. In short, reflect.SliceHeader could not be
used as plain struct.

See https://golang.org/pkg/unsafe/#Pointer

Signed-off-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
  • Loading branch information
cuonglm committed Nov 3, 2020
1 parent 7bb1944 commit c208f03
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions pkg/devicemapper/devmapper_wrapper.go
Expand Up @@ -150,12 +150,11 @@ func dmTaskGetDepsFct(task *cdmTask) *Deps {
}

// golang issue: https://github.com/golang/go/issues/11925
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(uintptr(unsafe.Pointer(Cdeps)) + unsafe.Sizeof(*Cdeps))),
Len: int(Cdeps.count),
Cap: int(Cdeps.count),
}
devices := *(*[]C.uint64_t)(unsafe.Pointer(&hdr))
var devices []C.uint64_t
devicesHdr := (*reflect.SliceHeader)(unsafe.Pointer(&devices))
devicesHdr.Data = uintptr(unsafe.Pointer(uintptr(unsafe.Pointer(Cdeps)) + unsafe.Sizeof(*Cdeps)))
devicesHdr.Len = int(Cdeps.count)
devicesHdr.Cap = int(Cdeps.count)

deps := &Deps{
Count: uint32(Cdeps.count),
Expand Down

0 comments on commit c208f03

Please sign in to comment.