Source file src/cmd/cgo/internal/testsanitizers/testdata/asan1_fail.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  /*
     8  #include <stdlib.h>
     9  #include <stdio.h>
    10  
    11  int *p;
    12  int* test() {
    13   p = (int *)malloc(2 * sizeof(int));
    14   free(p);
    15   return p;
    16  }
    17  */
    18  import "C"
    19  import "fmt"
    20  
    21  func main() {
    22  	// C passes Go an invalid pointer.
    23  	a := C.test()
    24  	// Use after free
    25  	*a = 2 // BOOM
    26  	// We shouldn't get here; asan should stop us first.
    27  	fmt.Println(*a)
    28  }
    29  

View as plain text