Skip to content

Commit 05d65e1

Browse files
committed
[pkg/bytes] Fix "go test -race"
Go checks the pointers used, and it is required to follow the rule #5 of "unsafe.Pointer" which requires to convert back to "unsafe.Pointer" no later than in the same line where it was converted to uintptr. Signed-off-by: Dmitrii Okunev <xaionaro@meta.com>
1 parent e43e22c commit 05d65e1

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

pkg/bytes/is_zero_filled_amd64.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,33 @@ import (
1515
// IsZeroFilled returns true if b consists of zeros only.
1616
func IsZeroFilled(b []byte) bool {
1717
hdr := (*reflect.SliceHeader)((unsafe.Pointer)(&b))
18-
data := hdr.Data
18+
data := unsafe.Pointer(hdr.Data)
1919
length := hdr.Len
20-
if data&0x07 != 0 {
20+
if length == 0 {
21+
return true
22+
}
23+
24+
if uintptr(data)&0x07 != 0 {
2125
// the data is not aligned, fallback to a simple way
2226
return isZeroFilledSimple(b)
2327
}
24-
dataEnd := hdr.Data + uintptr(length)
25-
dataWordsEnd := dataEnd & ^uintptr(0x07)
26-
for ; data < dataWordsEnd; data += 8 {
27-
if *(*uint64)(unsafe.Pointer(data)) != 0 {
28+
29+
dataEnd := uintptr(data) + uintptr(length)
30+
dataWordsEnd := uintptr(dataEnd) & ^uintptr(0x07)
31+
// example:
32+
//
33+
// 012345678901234567
34+
// wwwwwwwwWWWWWWWWtt : w -- word 0; W -- word 1; t -- tail
35+
// ^
36+
// |
37+
// +-- dataWordsEnd
38+
for ; uintptr(data) < dataWordsEnd; data = unsafe.Pointer(uintptr(data) + 8) {
39+
if *(*uint64)(data) != 0 {
2840
return false
2941
}
3042
}
31-
for ; data < dataEnd; data++ {
32-
if *(*uint8)(unsafe.Pointer(data)) != 0 {
43+
for ; uintptr(data) < dataEnd; data = unsafe.Pointer(uintptr(data) + 1) {
44+
if *(*uint8)(data) != 0 {
3345
return false
3446
}
3547
}

0 commit comments

Comments
 (0)