github.com/muesli/go@v0.0.0-20170208044820-e410d2a81ef2/misc/cgo/errors/malloc.go (about)

     1  // Copyright 2016 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  // Test that C.malloc does not return nil.
     6  
     7  package main
     8  
     9  // #include <stdlib.h>
    10  import "C"
    11  
    12  import (
    13  	"fmt"
    14  	"runtime"
    15  )
    16  
    17  func main() {
    18  	var size C.size_t
    19  	size--
    20  
    21  	// The Dragonfly libc succeeds when asked to allocate
    22  	// 0xffffffffffffffff bytes, so pass a different value that
    23  	// causes it to fail.
    24  	if runtime.GOOS == "dragonfly" {
    25  		size = C.size_t(0x7fffffff << (32 * (^uintptr(0) >> 63)))
    26  	}
    27  
    28  	p := C.malloc(size)
    29  	if p == nil {
    30  		fmt.Println("malloc: C.malloc returned nil")
    31  		// Just exit normally--the test script expects this
    32  		// program to crash, so exiting normally indicates failure.
    33  	}
    34  }