From 43ff72405cae657665a5a017aaf36d0782f2a186 Mon Sep 17 00:00:00 2001 From: Ruben Vermeersch Date: Fri, 24 Jan 2020 16:45:29 +0100 Subject: [PATCH] Add bounds checks to loops --- xor.go | 5 +++++ xor_amd64.go | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/xor.go b/xor.go index 791b171..680d425 100644 --- a/xor.go +++ b/xor.go @@ -12,6 +12,9 @@ func Bytes(dst, a, b []byte) int { if len(dst) < n { n = len(dst) } + _ = dst[n-1] + _ = a[n-1] + _ = b[n-1] for i := 0; i < n; i++ { dst[i] = a[i] ^ b[i] } @@ -26,6 +29,8 @@ func Byte(dst, a []byte, b byte) int { if len(dst) < n { n = len(dst) } + _ = dst[n-1] + _ = a[n-1] for i := 0; i < n; i++ { dst[i] = a[i] ^ b } diff --git a/xor_amd64.go b/xor_amd64.go index cb2ed16..a90a5d2 100644 --- a/xor_amd64.go +++ b/xor_amd64.go @@ -53,11 +53,17 @@ func xorBytesGeneric(dst, a, b []byte, n int) { dw := *(*[]uintptr)(unsafe.Pointer(&dst)) aw := *(*[]uintptr)(unsafe.Pointer(&a)) bw := *(*[]uintptr)(unsafe.Pointer(&b)) + _ = aw[w-1] + _ = bw[w-1] + _ = dw[w-1] for i := 0; i < w; i++ { dw[i] = aw[i] ^ bw[i] } } + _ = dst[n-1] + _ = a[n-1] + _ = b[n-1] for i := (n - n%wordSize); i < n; i++ { dst[i] = a[i] ^ b[i] } @@ -81,11 +87,15 @@ func Byte(dst, a []byte, b byte) int { if w > 0 { dw := *(*[]uintptr)(unsafe.Pointer(&dst)) aw := *(*[]uintptr)(unsafe.Pointer(&a)) + _ = aw[w-1] + _ = dw[w-1] for i := 0; i < w; i++ { dw[i] = aw[i] ^ bw } } + _ = dst[n-1] + _ = a[n-1] for i := (n - n%wordSize); i < n; i++ { dst[i] = a[i] ^ b }