asan2_fail.go 616 B

12345678910111213141516171819202122232425262728293031323334
  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. package main
  5. /*
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. int *p;
  9. int* f() {
  10. int i;
  11. p = (int *)malloc(5*sizeof(int));
  12. for (i = 0; i < 5; i++) {
  13. p[i] = i+10;
  14. }
  15. return p;
  16. }
  17. */
  18. import "C"
  19. import (
  20. "fmt"
  21. "unsafe"
  22. )
  23. func main() {
  24. a := C.f()
  25. q5 := (*C.int)(unsafe.Add(unsafe.Pointer(a), 4*5))
  26. // Access to C pointer out of bounds.
  27. *q5 = 100 // BOOM
  28. // We shouldn't get here; asan should stop us first.
  29. fmt.Printf("q5: %d, %x\n", *q5, q5)
  30. }