asan1_fail.go 500 B

12345678910111213141516171819202122232425262728
  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* test() {
  10. p = (int *)malloc(2 * sizeof(int));
  11. free(p);
  12. return p;
  13. }
  14. */
  15. import "C"
  16. import "fmt"
  17. func main() {
  18. // C passes Go an invalid pointer.
  19. a := C.test()
  20. // Use after free
  21. *a = 2 // BOOM
  22. // We shouldn't get here; asan should stop us first.
  23. fmt.Println(*a)
  24. }