]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: allow ^& uintptr arithmetic
authorJosh Bleecher Snyder <josharian@gmail.com>
Tue, 19 Jul 2016 00:33:05 +0000 (17:33 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Wed, 17 Aug 2016 20:48:57 +0000 (20:48 +0000)
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: Rob Pike <r@golang.org>
src/cmd/vet/testdata/unsafeptr.go
src/cmd/vet/unsafeptr.go

index e04856e234a7dee9e7e964b584a2b0761493e05e..ce852009ea7336cfc47e17fb35920b0c0381d5e8 100644 (file)
@@ -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
index a143e4d81c7d122c3975f705d48e8392fa2de324..cb2cc818897aa474df00d258f182387923a9e81f 100644 (file)
@@ -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)
                }
        }