From 3210885d21f90a1c266584fcc3bc37d0ba60b574 Mon Sep 17 00:00:00 2001 From: Sven Anderson Date: Sat, 2 Oct 2021 14:17:55 +0900 Subject: [PATCH] ptrguard: simplify pointer size assert Signed-off-by: Sven Anderson --- internal/cutil/ptrguard.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/cutil/ptrguard.go b/internal/cutil/ptrguard.go index 9be6424..27a4c5e 100644 --- a/internal/cutil/ptrguard.go +++ b/internal/cutil/ptrguard.go @@ -60,10 +60,10 @@ func (v *PtrGuard) Release() { // The uintptrPtr() helper function below assumes that uintptr has the same size // as a pointer, although in theory it could be larger. Therefore we use this // constant expression to assert size equality as a safeguard at compile time. -// How it works: the difference of both sizes is converted into an 8 bit value -// and left-bit-shifted by 8. This always creates an overflow error at compile -// time, if the difference of the sizes is not 0. -const _ = uint8(unsafe.Sizeof(uintptr(0))-PtrSize) << 8 // size assert +// How it works: if sizes are different, either the inner or outer expression is +// negative, which always fails with "constant ... overflows uintptr", because +// unsafe.Sizeof() is a uintptr typed constant. +const _ = -(unsafe.Sizeof(uintptr(0)) - PtrSize) // size assert func uintptrPtr(p *CPtr) *uintptr { return (*uintptr)(unsafe.Pointer(p)) }