Source file src/reflect/nih_test.go

     1  // Copyright 2009 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  //go:build cgo
     6  
     7  package reflect_test
     8  
     9  import (
    10  	. "reflect"
    11  	"runtime/cgo"
    12  	"testing"
    13  	"unsafe"
    14  )
    15  
    16  type nih struct {
    17  	_ cgo.Incomplete
    18  	x int
    19  }
    20  
    21  var global_nih = nih{x: 7}
    22  
    23  func TestNotInHeapDeref(t *testing.T) {
    24  	// See issue 48399.
    25  	v := ValueOf((*nih)(nil))
    26  	v.Elem()
    27  	shouldPanic("reflect: call of reflect.Value.Field on zero Value", func() { v.Elem().Field(0) })
    28  
    29  	v = ValueOf(&global_nih)
    30  	if got := v.Elem().Field(1).Int(); got != 7 {
    31  		t.Fatalf("got %d, want 7", got)
    32  	}
    33  
    34  	v = ValueOf((*nih)(unsafe.Pointer(new(int))))
    35  	shouldPanic("reflect: reflect.Value.Elem on an invalid notinheap pointer", func() { v.Elem() })
    36  	shouldPanic("reflect: reflect.Value.Pointer on an invalid notinheap pointer", func() { v.Pointer() })
    37  	shouldPanic("reflect: reflect.Value.UnsafePointer on an invalid notinheap pointer", func() { v.UnsafePointer() })
    38  }
    39  

View as plain text