PR tree-optimization/86274 - SEGFAULT when logging std::to_string(NAN)
[official-gcc.git] / libgo / go / cmd / vet / testdata / unsafeptr.go
blobce852009ea7336cfc47e17fb35920b0c0381d5e8
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.
5 package testdata
7 import (
8 "reflect"
9 "unsafe"
12 func f() {
13 var x unsafe.Pointer
14 var y uintptr
15 x = unsafe.Pointer(y) // ERROR "possible misuse of unsafe.Pointer"
16 y = uintptr(x)
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(1 + uintptr(x)) // ERROR "possible misuse of unsafe.Pointer"
22 x = unsafe.Pointer(uintptr(x) + uintptr(x)) // ERROR "possible misuse of unsafe.Pointer"
23 x = unsafe.Pointer(uintptr(x) - 1)
24 x = unsafe.Pointer(1 - uintptr(x)) // ERROR "possible misuse of unsafe.Pointer"
25 x = unsafe.Pointer(uintptr(x) &^ 3)
26 x = unsafe.Pointer(1 &^ uintptr(x)) // ERROR "possible misuse of unsafe.Pointer"
28 // certain uses of reflect are okay
29 var v reflect.Value
30 x = unsafe.Pointer(v.Pointer())
31 x = unsafe.Pointer(v.UnsafeAddr())
32 var s1 *reflect.StringHeader
33 x = unsafe.Pointer(s1.Data)
34 var s2 *reflect.SliceHeader
35 x = unsafe.Pointer(s2.Data)
36 var s3 reflect.StringHeader
37 x = unsafe.Pointer(s3.Data) // ERROR "possible misuse of unsafe.Pointer"
38 var s4 reflect.SliceHeader
39 x = unsafe.Pointer(s4.Data) // ERROR "possible misuse of unsafe.Pointer"
41 // but only in reflect
42 var vv V
43 x = unsafe.Pointer(vv.Pointer()) // ERROR "possible misuse of unsafe.Pointer"
44 x = unsafe.Pointer(vv.UnsafeAddr()) // ERROR "possible misuse of unsafe.Pointer"
45 var ss1 *StringHeader
46 x = unsafe.Pointer(ss1.Data) // ERROR "possible misuse of unsafe.Pointer"
47 var ss2 *SliceHeader
48 x = unsafe.Pointer(ss2.Data) // ERROR "possible misuse of unsafe.Pointer"
52 type V interface {
53 Pointer() uintptr
54 UnsafeAddr() uintptr
57 type StringHeader struct {
58 Data uintptr
61 type SliceHeader struct {
62 Data uintptr