aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsina <sina@snix.ir>2022-07-30 19:01:05 +0430
committersina <sina@snix.ir>2022-07-30 19:01:05 +0430
commite697fd436cabccb32114ebb74d1de9b9516dead3 (patch)
treee25f2719602779da65d8874b178fae7396c8f6e3
parentf3dc60d49043cdf866c69a5df8bfe665a81e874b (diff)
panics and overlap
-rw-r--r--overlap.go18
-rw-r--r--purelap.go18
-rw-r--r--rabbit.go9
3 files changed, 45 insertions, 0 deletions
diff --git a/overlap.go b/overlap.go
new file mode 100644
index 0000000..564736b
--- /dev/null
+++ b/overlap.go
@@ -0,0 +1,18 @@
+//go:build !purego
+
+package rabbitio
+
+import "unsafe"
+
+func AnyOverlap(x, y []byte) bool {
+ return len(x) > 0 && len(y) > 0 &&
+ uintptr(unsafe.Pointer(&x[0])) <= uintptr(unsafe.Pointer(&y[len(y)-1])) &&
+ uintptr(unsafe.Pointer(&y[0])) <= uintptr(unsafe.Pointer(&x[len(x)-1]))
+}
+
+func InexactOverlap(x, y []byte) bool {
+ if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] {
+ return false
+ }
+ return AnyOverlap(x, y)
+}
diff --git a/purelap.go b/purelap.go
new file mode 100644
index 0000000..debd418
--- /dev/null
+++ b/purelap.go
@@ -0,0 +1,18 @@
+//go:build purego
+
+package rabbitio
+
+import "reflect"
+
+func AnyOverlap(x, y []byte) bool {
+ return len(x) > 0 && len(y) > 0 &&
+ reflect.ValueOf(&x[0]).Pointer() <= reflect.ValueOf(&y[len(y)-1]).Pointer() &&
+ reflect.ValueOf(&y[0]).Pointer() <= reflect.ValueOf(&x[len(x)-1]).Pointer()
+}
+
+func InexactOverlap(x, y []byte) bool {
+ if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] {
+ return false
+ }
+ return AnyOverlap(x, y)
+}
diff --git a/rabbit.go b/rabbit.go
index 3b2ed12..3d77daa 100644
--- a/rabbit.go
+++ b/rabbit.go
@@ -136,6 +136,15 @@ func (r *rabbitCipher) extract() {
// XORKeyStream read from src and perform xor on every elemnt of src and
// write result on dst
func (r *rabbitCipher) XORKeyStream(dst, src []byte) {
+
+ if len(dst) < len(src) {
+ panic("rabbitio: output smaller than input")
+ }
+
+ if InexactOverlap(dst, src) {
+ panic("rabbitio: invalid buffer memory overlap")
+ }
+
for i := range src {
if len(r.ks) == 0x00 {
r.extract()

Snix LLC Git Repository Holder Copyright(C) 2022 All Rights Reserved Email To Snix.IR