Add bounds checks to loops

This commit is contained in:
Ruben Vermeersch
2020-01-24 16:45:29 +01:00
parent 7d8727055c
commit 43ff72405c
2 changed files with 15 additions and 0 deletions
+5
View File
@@ -12,6 +12,9 @@ func Bytes(dst, a, b []byte) int {
if len(dst) < n { if len(dst) < n {
n = len(dst) n = len(dst)
} }
_ = dst[n-1]
_ = a[n-1]
_ = b[n-1]
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
dst[i] = a[i] ^ b[i] dst[i] = a[i] ^ b[i]
} }
@@ -26,6 +29,8 @@ func Byte(dst, a []byte, b byte) int {
if len(dst) < n { if len(dst) < n {
n = len(dst) n = len(dst)
} }
_ = dst[n-1]
_ = a[n-1]
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
dst[i] = a[i] ^ b dst[i] = a[i] ^ b
} }
+10
View File
@@ -53,11 +53,17 @@ func xorBytesGeneric(dst, a, b []byte, n int) {
dw := *(*[]uintptr)(unsafe.Pointer(&dst)) dw := *(*[]uintptr)(unsafe.Pointer(&dst))
aw := *(*[]uintptr)(unsafe.Pointer(&a)) aw := *(*[]uintptr)(unsafe.Pointer(&a))
bw := *(*[]uintptr)(unsafe.Pointer(&b)) bw := *(*[]uintptr)(unsafe.Pointer(&b))
_ = aw[w-1]
_ = bw[w-1]
_ = dw[w-1]
for i := 0; i < w; i++ { for i := 0; i < w; i++ {
dw[i] = aw[i] ^ bw[i] dw[i] = aw[i] ^ bw[i]
} }
} }
_ = dst[n-1]
_ = a[n-1]
_ = b[n-1]
for i := (n - n%wordSize); i < n; i++ { for i := (n - n%wordSize); i < n; i++ {
dst[i] = a[i] ^ b[i] dst[i] = a[i] ^ b[i]
} }
@@ -81,11 +87,15 @@ func Byte(dst, a []byte, b byte) int {
if w > 0 { if w > 0 {
dw := *(*[]uintptr)(unsafe.Pointer(&dst)) dw := *(*[]uintptr)(unsafe.Pointer(&dst))
aw := *(*[]uintptr)(unsafe.Pointer(&a)) aw := *(*[]uintptr)(unsafe.Pointer(&a))
_ = aw[w-1]
_ = dw[w-1]
for i := 0; i < w; i++ { for i := 0; i < w; i++ {
dw[i] = aw[i] ^ bw dw[i] = aw[i] ^ bw
} }
} }
_ = dst[n-1]
_ = a[n-1]
for i := (n - n%wordSize); i < n; i++ { for i := (n - n%wordSize); i < n; i++ {
dst[i] = a[i] ^ b dst[i] = a[i] ^ b
} }