Commit 752e1615 authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

cmd/vet: allow ^& uintptr arithmetic

The unsafe.Pointer check allows adding to
and subtracting from uintptrs in order to do
arithmetic.

Some code needs to round uintptrs.
Allow &^ for that purpose.

Updates #11041

Change-Id: Ib90dd2954bb6c78427058271e13f2ce4c4af38fb
Reviewed-on: https://go-review.googlesource.com/27156
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRob Pike <r@golang.org>
parent ebcd1797
......@@ -15,13 +15,15 @@ func f() {
x = unsafe.Pointer(y) // ERROR "possible misuse of unsafe.Pointer"
y = uintptr(x)
// only allowed pointer arithmetic is ptr +/- num.
// only allowed pointer arithmetic is ptr +/-/&^ num.
// num+ptr is technically okay but still flagged: write ptr+num instead.
x = unsafe.Pointer(uintptr(x) + 1)
x = unsafe.Pointer(1 + uintptr(x)) // ERROR "possible misuse of unsafe.Pointer"
x = unsafe.Pointer(uintptr(x) + uintptr(x)) // ERROR "possible misuse of unsafe.Pointer"
x = unsafe.Pointer(uintptr(x) - 1)
x = unsafe.Pointer(1 - uintptr(x)) // ERROR "possible misuse of unsafe.Pointer"
x = unsafe.Pointer(uintptr(x) &^ 3)
x = unsafe.Pointer(1 &^ uintptr(x)) // ERROR "possible misuse of unsafe.Pointer"
// certain uses of reflect are okay
var v reflect.Value
......
......@@ -89,7 +89,7 @@ func (f *File) isSafeUintptr(x ast.Expr) bool {
case *ast.BinaryExpr:
switch x.Op {
case token.ADD, token.SUB:
case token.ADD, token.SUB, token.AND_NOT:
return f.isSafeUintptr(x.X) && !f.isSafeUintptr(x.Y)
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment