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

     1  // Copyright 2025 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  
    10  int* test() {
    11     return malloc(sizeof(int));
    12  }
    13  
    14  void clearStack(int n) {
    15    if (n > 0) {
    16      clearStack(n - 1);
    17    }
    18  }
    19  
    20  */
    21  import "C"
    22  
    23  var p *C.int
    24  
    25  //go:noinline
    26  func F() {
    27  	p = C.test()
    28  }
    29  
    30  func clearStack(n int) {
    31  	if n > 0 {
    32  		clearStack(n - 1)
    33  	}
    34  }
    35  
    36  func main() {
    37  	// Test should pass: memory allocated by C does not leak
    38  	// because a Go global variable points to it.
    39  	F()
    40  	clearStack(100)
    41  	C.clearStack(100)
    42  }
    43  

View as plain text