Source file src/cmd/cgo/internal/testsanitizers/testdata/asan2_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* f() {
    13    int i;
    14    p = (int *)malloc(5*sizeof(int));
    15    for (i = 0; i < 5; i++) {
    16      p[i] = i+10;
    17    }
    18    return p;
    19  }
    20  */
    21  import "C"
    22  import (
    23  	"fmt"
    24  	"unsafe"
    25  )
    26  
    27  func main() {
    28  	a := C.f()
    29  	q5 := (*C.int)(unsafe.Add(unsafe.Pointer(a), 4*5))
    30  	// Access to C pointer out of bounds.
    31  	*q5 = 100 // BOOM
    32  	// We shouldn't get here; asan should stop us first.
    33  	fmt.Printf("q5: %d, %x\n", *q5, q5)
    34  }
    35  

View as plain text