| 1 | // Copyright 2014 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 | package a |
| 6 | |
| 7 | import ( |
| 8 | "reflect" |
| 9 | "unsafe" |
| 10 | ) |
| 11 | |
| 12 | func f() { |
| 13 | var x unsafe.Pointer |
| 14 | var y uintptr |
| 15 | x = unsafe.Pointer(y) // want "possible misuse of unsafe.Pointer" |
| 16 | y = uintptr(x) |
| 17 | |
| 18 | // only allowed pointer arithmetic is ptr +/-/&^ num. |
| 19 | // num+ptr is technically okay but still flagged: write ptr+num instead. |
| 20 | x = unsafe.Pointer(uintptr(x) + 1) |
| 21 | x = unsafe.Pointer(((uintptr((x))) + 1)) |
| 22 | x = unsafe.Pointer(1 + uintptr(x)) // want "possible misuse of unsafe.Pointer" |
| 23 | x = unsafe.Pointer(uintptr(x) + uintptr(x)) // want "possible misuse of unsafe.Pointer" |
| 24 | x = unsafe.Pointer(uintptr(x) - 1) |
| 25 | x = unsafe.Pointer(1 - uintptr(x)) // want "possible misuse of unsafe.Pointer" |
| 26 | x = unsafe.Pointer(uintptr(x) &^ 3) |
| 27 | x = unsafe.Pointer(1 &^ uintptr(x)) // want "possible misuse of unsafe.Pointer" |
| 28 | |
| 29 | // certain uses of reflect are okay |
| 30 | var v reflect.Value |
| 31 | x = unsafe.Pointer(v.Pointer()) |
| 32 | x = unsafe.Pointer(v.Pointer() + 1) // want "possible misuse of unsafe.Pointer" |
| 33 | x = unsafe.Pointer(v.UnsafeAddr()) |
| 34 | x = unsafe.Pointer((v.UnsafeAddr())) |
| 35 | var s1 *reflect.StringHeader |
| 36 | x = unsafe.Pointer(s1.Data) |
| 37 | x = unsafe.Pointer(s1.Data + 1) // want "possible misuse of unsafe.Pointer" |
| 38 | var s2 *reflect.SliceHeader |
| 39 | x = unsafe.Pointer(s2.Data) |
| 40 | var s3 reflect.StringHeader |
| 41 | x = unsafe.Pointer(s3.Data) // want "possible misuse of unsafe.Pointer" |
| 42 | var s4 reflect.SliceHeader |
| 43 | x = unsafe.Pointer(s4.Data) // want "possible misuse of unsafe.Pointer" |
| 44 | |
| 45 | // but only in reflect |
| 46 | var vv V |
| 47 | x = unsafe.Pointer(vv.Pointer()) // want "possible misuse of unsafe.Pointer" |
| 48 | x = unsafe.Pointer(vv.UnsafeAddr()) // want "possible misuse of unsafe.Pointer" |
| 49 | var ss1 *StringHeader |
| 50 | x = unsafe.Pointer(ss1.Data) // want "possible misuse of unsafe.Pointer" |
| 51 | var ss2 *SliceHeader |
| 52 | x = unsafe.Pointer(ss2.Data) // want "possible misuse of unsafe.Pointer" |
| 53 | |
| 54 | } |
| 55 | |
| 56 | type V interface { |
| 57 | Pointer() uintptr |
| 58 | UnsafeAddr() uintptr |
| 59 | } |
| 60 | |
| 61 | type StringHeader struct { |
| 62 | Data uintptr |
| 63 | } |
| 64 | |
| 65 | type SliceHeader struct { |
| 66 | Data uintptr |
| 67 | } |
| 68 |
Members