aboutsummaryrefslogtreecommitdiff
path: root/misc/cgo/errors/testdata/malloc.go
blob: 65da0208b9708f8115abcdc00733acff25358ddc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Test that C.malloc does not return nil.

package main

// #include <stdlib.h>
import "C"

import (
	"fmt"
	"runtime"
)

func main() {
	var size C.size_t
	size--

	// The Dragonfly libc succeeds when asked to allocate
	// 0xffffffffffffffff bytes, so pass a different value that
	// causes it to fail.
	if runtime.GOOS == "dragonfly" {
		size = C.size_t(0x7fffffff << (32 * (^uintptr(0) >> 63)))
	}

	p := C.malloc(size)
	if p == nil {
		fmt.Println("malloc: C.malloc returned nil")
		// Just exit normally--the test script expects this
		// program to crash, so exiting normally indicates failure.
	}
}