Skip to content

Commit

Permalink
Fix deepsource: GSC-G103 Function call made to an unsafe package (#1850)
Browse files Browse the repository at this point in the history
Co-authored-by: Kiichiro YUKAWA <kyukawa315@gmail.com>
  • Loading branch information
datelier and vankichi authored Nov 21, 2022
1 parent 91fb575 commit f6f55f9
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions hack/benchmark/assets/x1b/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func open(fname string, elementSize int) (f *file, err error) {
return nil, err
}

// skipcq: GSC-G103
dim := int(*(*int32)(unsafe.Pointer(&mem[0])))
block := headerSize + dim*elementSize
return &file{
Expand Down Expand Up @@ -128,6 +129,7 @@ func (bv *bvecs) LoadUint8(i int) ([]uint8, error) {
if err != nil {
return nil, err
}
// skipcq: GSC-G103
return ((*[1 << 26]uint8)(unsafe.Pointer(&buf[0])))[:bv.dim:bv.dim], nil
}

Expand All @@ -140,6 +142,7 @@ func (fv *fvecs) LoadFloat32(i int) ([]float32, error) {
if err != nil {
return nil, err
}
// skipcq: GSC-G103
return ((*[1 << 26]float32)(unsafe.Pointer(&buf[0])))[:fv.dim:fv.dim], nil
}

Expand All @@ -152,6 +155,7 @@ func (iv *ivecs) LoadInt32(i int) ([]int32, error) {
if err != nil {
return nil, err
}
// skipcq: GSC-G103
return ((*[1 << 26]int32)(unsafe.Pointer(&buf[0])))[:iv.dim:iv.dim], nil
}

Expand Down
8 changes: 8 additions & 0 deletions internal/conv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (

// Btoa converts from byte slice to string.
func Btoa(b []byte) (s string) {
// skipcq: GSC-G103
slh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
// skipcq: GSC-G103
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
sh.Data = slh.Data
sh.Len = slh.Len
Expand All @@ -34,7 +36,9 @@ func Btoa(b []byte) (s string) {

// Atobs converts from string to byte slice.
func Atob(s string) (b []byte) {
// skipcq: GSC-G103
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
// skipcq: GSC-G103
slh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
slh.Data = sh.Data
slh.Len = sh.Len
Expand All @@ -45,9 +49,13 @@ func Atob(s string) (b []byte) {
// F32stos converts from float32 slice to type string.
func F32stos(fs []float32) (s string) {
lf := 4 * len(fs)
// skipcq: GSC-G103
buf := (*(*[1]byte)(unsafe.Pointer(&(fs[0]))))[:]
// skipcq: GSC-G103
addr := unsafe.Pointer(&buf)
// skipcq: GSC-G103
(*(*int)(unsafe.Pointer(uintptr(addr) + uintptr(8)))) = lf
// skipcq: GSC-G103
(*(*int)(unsafe.Pointer(uintptr(addr) + uintptr(16)))) = lf
return Btoa(buf)
}
Expand Down
4 changes: 4 additions & 0 deletions internal/net/grpc/grpcconns.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ type readOnlyGrpcConns struct {
amended bool
}

// skipcq: GSC-G103
var expungedGrpcConns = unsafe.Pointer(new(pool.Conn))

type entryGrpcConns struct {
p unsafe.Pointer
}

func newEntryGrpcConns(i pool.Conn) *entryGrpcConns {
// skipcq: GSC-G103
return &entryGrpcConns{p: unsafe.Pointer(&i)}
}

Expand Down Expand Up @@ -105,6 +107,7 @@ func (e *entryGrpcConns) tryStore(i *pool.Conn) bool {
if p == expungedGrpcConns {
return false
}
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
return true
}
Expand All @@ -116,6 +119,7 @@ func (e *entryGrpcConns) unexpungeLocked() (wasExpunged bool) {
}

func (e *entryGrpcConns) storeLocked(i *pool.Conn) {
// skipcq: GSC-G103
atomic.StorePointer(&e.p, unsafe.Pointer(i))
}

Expand Down
1 change: 1 addition & 0 deletions internal/strings/strings_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func randStr(n int) string {
cache >>= rs6LetterIdxBits
remain--
}
// skipcq: GSC-G103
return *(*string)(unsafe.Pointer(&b))
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/agent/core/ngt/service/kvs/ou.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ type readOnlyOu struct {
amended bool
}

// skipcq: GSC-G103
var expungedOu = unsafe.Pointer(new(string))

type entryOu struct {
p unsafe.Pointer
}

func newEntryOu(i string) *entryOu {
// skipcq: GSC-G103
return &entryOu{p: unsafe.Pointer(&i)}
}

Expand Down Expand Up @@ -98,6 +100,7 @@ func (e *entryOu) tryStore(i *string) bool {
if p == expungedOu {
return false
}
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
return true
}
Expand All @@ -109,6 +112,7 @@ func (e *entryOu) unexpungeLocked() (wasExpunged bool) {
}

func (e *entryOu) storeLocked(i *string) {
// skipcq: GSC-G103
atomic.StorePointer(&e.p, unsafe.Pointer(i))
}

Expand Down Expand Up @@ -152,6 +156,7 @@ func (e *entryOu) tryLoadOrStore(i string) (actual string, loaded, ok bool) {
}
ic := i
for {
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, nil, unsafe.Pointer(&ic)) {
return i, false, true
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/agent/core/ngt/service/kvs/uo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ type readOnlyUo struct {
amended bool
}

// skipcq: GSC-G103
var expungedUo = unsafe.Pointer(new(uint32))

type entryUo struct {
p unsafe.Pointer
}

func newEntryUo(i uint32) *entryUo {
// skipcq: GSC-G103
return &entryUo{p: unsafe.Pointer(&i)}
}

Expand Down Expand Up @@ -98,6 +100,7 @@ func (e *entryUo) tryStore(i *uint32) bool {
if p == expungedUo {
return false
}
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
return true
}
Expand All @@ -109,6 +112,7 @@ func (e *entryUo) unexpungeLocked() (wasExpunged bool) {
}

func (e *entryUo) storeLocked(i *uint32) {
// skipcq: GSC-G103
atomic.StorePointer(&e.p, unsafe.Pointer(i))
}

Expand Down Expand Up @@ -152,6 +156,7 @@ func (e *entryUo) tryLoadOrStore(i uint32) (actual uint32, loaded, ok bool) {
}
ic := i
for {
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, nil, unsafe.Pointer(&ic)) {
return i, false, true
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/agent/core/ngt/service/vqueue/undeleted_index_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ type readOnlyUdim struct {
amended bool
}

// skipcq: GSC-G103
var expungedUdim = unsafe.Pointer(new(int64))

type entryUdim struct {
p unsafe.Pointer
}

func newEntryUdim(i int64) *entryUdim {
// skipcq: GSC-G103
return &entryUdim{p: unsafe.Pointer(&i)}
}

Expand Down Expand Up @@ -99,6 +101,7 @@ func (e *entryUdim) tryStore(i *int64) bool {
if p == expungedUdim {
return false
}
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
return true
}
Expand All @@ -110,6 +113,7 @@ func (e *entryUdim) unexpungeLocked() (wasExpunged bool) {
}

func (e *entryUdim) storeLocked(i *int64) {
// skipcq: GSC-G103
atomic.StorePointer(&e.p, unsafe.Pointer(i))
}

Expand Down Expand Up @@ -157,6 +161,7 @@ func (e *entryUdim) tryLoadOrStore(i int64) (actual int64, loaded, ok bool) {

ic := i
for {
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, nil, unsafe.Pointer(&ic)) {
return i, false, true
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/agent/core/ngt/service/vqueue/uninserted_index_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ type readOnlyUiim struct {
amended bool
}

// skipcq: GSC-G103
var expungedUiim = unsafe.Pointer(new(index))

type entryUiim struct {
p unsafe.Pointer
}

func newEntryUiim(i index) *entryUiim {
// skipcq: GSC-G103
return &entryUiim{p: unsafe.Pointer(&i)}
}

Expand Down Expand Up @@ -105,6 +107,7 @@ func (e *entryUiim) tryStore(i *index) bool {
if p == expungedUiim {
return false
}
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
return true
}
Expand All @@ -116,6 +119,7 @@ func (e *entryUiim) unexpungeLocked() (wasExpunged bool) {
}

func (e *entryUiim) storeLocked(i *index) {
// skipcq: GSC-G103
atomic.StorePointer(&e.p, unsafe.Pointer(i))
}

Expand Down Expand Up @@ -163,6 +167,7 @@ func (e *entryUiim) tryLoadOrStore(i index) (actual index, loaded, ok bool) {

ic := i
for {
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, nil, unsafe.Pointer(&ic)) {
return i, false, true
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/discoverer/k8s/service/nodemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type readOnlyNodeMap struct {

// expunged is an arbitrary pointer that marks entries which have been deleted
// from the dirty map.
// skipcq: GSC-G103
var expungedNodeMap = unsafe.Pointer(new(*node.Node))

// An entry is a slot in the map corresponding to a particular key.
Expand All @@ -108,6 +109,7 @@ type entryNodeMap struct {
}

func newEntryNodeMap(i *node.Node) *entryNodeMap {
// skipcq: GSC-G103
return &entryNodeMap{p: unsafe.Pointer(&i)}
}

Expand Down Expand Up @@ -187,6 +189,7 @@ func (e *entryNodeMap) tryStore(i **node.Node) bool {
if p == expungedNodeMap {
return false
}
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
return true
}
Expand All @@ -205,6 +208,7 @@ func (e *entryNodeMap) unexpungeLocked() (wasExpunged bool) {
//
// The entry must be known not to be expunged.
func (e *entryNodeMap) storeLocked(i **node.Node) {
// skipcq: GSC-G103
atomic.StorePointer(&e.p, unsafe.Pointer(i))
}

Expand Down Expand Up @@ -265,6 +269,7 @@ func (e *entryNodeMap) tryLoadOrStore(i *node.Node) (actual *node.Node, loaded,
// shouldn't bother heap-allocating.
ic := i
for {
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, nil, unsafe.Pointer(&ic)) {
return i, false, true
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/discoverer/k8s/service/nodemetricsmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type readOnlyNodeMetricsMap struct {

// expunged is an arbitrary pointer that marks entries which have been deleted
// from the dirty map.
// skipcq: GSC-G103
var expungedNodeMetricsMap = unsafe.Pointer(new(mnode.Node))

// An entry is a slot in the map corresponding to a particular key.
Expand All @@ -108,6 +109,7 @@ type entryNodeMetricsMap struct {
}

func newEntryNodeMetricsMap(i mnode.Node) *entryNodeMetricsMap {
// skipcq: GSC-G103
return &entryNodeMetricsMap{p: unsafe.Pointer(&i)}
}

Expand Down Expand Up @@ -187,6 +189,7 @@ func (e *entryNodeMetricsMap) tryStore(i *mnode.Node) bool {
if p == expungedNodeMetricsMap {
return false
}
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
return true
}
Expand All @@ -205,6 +208,7 @@ func (e *entryNodeMetricsMap) unexpungeLocked() (wasExpunged bool) {
//
// The entry must be known not to be expunged.
func (e *entryNodeMetricsMap) storeLocked(i *mnode.Node) {
// skipcq: GSC-G103
atomic.StorePointer(&e.p, unsafe.Pointer(i))
}

Expand Down Expand Up @@ -265,6 +269,7 @@ func (e *entryNodeMetricsMap) tryLoadOrStore(i mnode.Node) (actual mnode.Node, l
// shouldn't bother heap-allocating.
ic := i
for {
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, nil, unsafe.Pointer(&ic)) {
return i, false, true
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/discoverer/k8s/service/podmetricsmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type readOnlyPodMetricsMap struct {

// expunged is an arbitrary pointer that marks entries which have been deleted
// from the dirty map.
// skipcq: GSC-G103
var expungedPodMetricsMap = unsafe.Pointer(new(mpod.Pod))

// An entry is a slot in the map corresponding to a particular key.
Expand All @@ -108,6 +109,7 @@ type entryPodMetricsMap struct {
}

func newEntryPodMetricsMap(i mpod.Pod) *entryPodMetricsMap {
// skipcq: GSC-G103
return &entryPodMetricsMap{p: unsafe.Pointer(&i)}
}

Expand Down Expand Up @@ -187,6 +189,7 @@ func (e *entryPodMetricsMap) tryStore(i *mpod.Pod) bool {
if p == expungedPodMetricsMap {
return false
}
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
return true
}
Expand All @@ -205,6 +208,7 @@ func (e *entryPodMetricsMap) unexpungeLocked() (wasExpunged bool) {
//
// The entry must be known not to be expunged.
func (e *entryPodMetricsMap) storeLocked(i *mpod.Pod) {
// skipcq: GSC-G103
atomic.StorePointer(&e.p, unsafe.Pointer(i))
}

Expand Down Expand Up @@ -265,6 +269,7 @@ func (e *entryPodMetricsMap) tryLoadOrStore(i mpod.Pod) (actual mpod.Pod, loaded
// shouldn't bother heap-allocating.
ic := i
for {
// skipcq: GSC-G103
if atomic.CompareAndSwapPointer(&e.p, nil, unsafe.Pointer(&ic)) {
return i, false, true
}
Expand Down
Loading

0 comments on commit f6f55f9

Please sign in to comment.