]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: check shift calculations with "unsafe" package
authorAliaksandr Valialkin <valyala@gmail.com>
Mon, 13 Mar 2017 16:29:46 +0000 (18:29 +0200)
committerJosh Bleecher Snyder <josharian@gmail.com>
Mon, 13 Mar 2017 22:30:27 +0000 (22:30 +0000)
vet should properly handle shift calculations via "unsafe"
package after the CL 37950.

Change-Id: I7737f2e656a5166337a17b92db46a0997f2a4e0e
Reviewed-on: https://go-review.googlesource.com/38064
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/vet/shift.go
src/cmd/vet/testdata/shift.go

index 200e20fd9dbc87a8c7f8a9634e3f3f196789eb0c..17531bfc755d9b30b5775106e1f54f9b21861cad 100644 (file)
@@ -48,28 +48,6 @@ func checkLongShift(f *File, node ast.Node, x, y ast.Expr) {
                return
        }
 
-       // Ignore shifts where the shift amount is calculated using unsafe.
-       // These are used for bit-twiddling tricks.
-       var hasUnsafe bool
-       ast.Inspect(y, func(n ast.Node) bool {
-               sel, ok := n.(*ast.SelectorExpr)
-               if !ok {
-                       return true
-               }
-               pkg, ok := sel.X.(*ast.Ident)
-               if !ok {
-                       return true
-               }
-               if pkg.Name == "unsafe" {
-                       hasUnsafe = true
-                       return false
-               }
-               return true
-       })
-       if hasUnsafe {
-               return
-       }
-
        v := f.pkg.types[y].Value
        if v == nil {
                return
index 40c8c8aa4f6bccffc4ca5d8e293ad76048a5d03e..d43b941f126d0a1249b06741ac44dbdbf2e07b64 100644 (file)
@@ -102,5 +102,8 @@ func ShiftTest() {
        const oneIf64Bit = ^uint(0) >> 63 // allow large shifts of constants; they are used for 32/64 bit compatibility tricks
 
        var h uintptr
-       h = h<<8 | (h >> (8 * (unsafe.Sizeof(h) - 1))) // shifts by unsafe amounts are safe
+       h = h<<8 | (h >> (8 * (unsafe.Sizeof(h) - 1)))
+       h <<= 8 * unsafe.Sizeof(h) // ERROR "too small for shift"
+       h >>= 7 * unsafe.Alignof(h)
+       h >>= 8 * unsafe.Alignof(h) // ERROR "too small for shift"
 }