podman
1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5//go:build !purego
6
7// Package alias implements memory aliasing tests.
8package alias9
10import "unsafe"11
12// AnyOverlap reports whether x and y share memory at any (not necessarily
13// corresponding) index. The memory beyond the slice length is ignored.
14func AnyOverlap(x, y []byte) bool {15return len(x) > 0 && len(y) > 0 &&16uintptr(unsafe.Pointer(&x[0])) <= uintptr(unsafe.Pointer(&y[len(y)-1])) &&17uintptr(unsafe.Pointer(&y[0])) <= uintptr(unsafe.Pointer(&x[len(x)-1]))18}
19
20// InexactOverlap reports whether x and y share memory at any non-corresponding
21// index. The memory beyond the slice length is ignored. Note that x and y can
22// have different lengths and still not have any inexact overlap.
23//
24// InexactOverlap can be used to implement the requirements of the crypto/cipher
25// AEAD, Block, BlockMode and Stream interfaces.
26func InexactOverlap(x, y []byte) bool {27if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] {28return false29}30return AnyOverlap(x, y)31}
32