Source file src/cmd/cgo/internal/testsanitizers/testdata/asan_unsafe_fail2.go

     1  // Copyright 2022 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 main
     6  
     7  import (
     8  	"fmt"
     9  	"unsafe"
    10  )
    11  
    12  func main() {
    13  	a := 1
    14  	b := 2
    15  	c := add(a, b)
    16  	d := a + b
    17  	fmt.Println(c, d)
    18  }
    19  
    20  //go:noinline
    21  func add(a1, b1 int) (ret int) {
    22  	// The return value
    23  	// When -asan is enabled, the unsafe.Pointer(&ret) conversion is escaping.
    24  	var p *int = (*int)(unsafe.Add(unsafe.Pointer(&ret), 1*unsafe.Sizeof(int(1))))
    25  	*p = 123 // BOOM
    26  	ret = a1 + b1
    27  	return
    28  }
    29  

View as plain text