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

     1  // Copyright 2021 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  // The -fsanitize=address option of C compier can detect stack-use-after-return bugs.
     8  // In the following program, the local variable 'local' was moved to heap by the Go
     9  // compiler because foo() is returning the reference to 'local', and return stack of
    10  // foo() will be invalid. Thus for main() to use the reference to 'local', the 'local'
    11  // must be available even after foo() has finished. Therefore, Go has no such issue.
    12  
    13  import "fmt"
    14  
    15  var ptr *int
    16  
    17  func main() {
    18  	foo()
    19  	fmt.Printf("ptr=%x, %v", *ptr, ptr)
    20  }
    21  
    22  func foo() {
    23  	var local int
    24  	local = 1
    25  	ptr = &local // local is moved to heap.
    26  }
    27  

View as plain text