File tree 3 files changed +39
-0
lines changed
3 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
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
+ )
15
+
16
+ func main () {
17
+ p := C .malloc (C .size_t (^ uintptr (0 )))
18
+ if p == nil {
19
+ fmt .Println ("malloc: C.malloc returned nil" )
20
+ // Just exit normally--the test script expects this
21
+ // program to crash, so exiting normally indicates failure.
22
+ }
23
+ }
Original file line number Diff line number Diff line change @@ -59,5 +59,15 @@ if ! go run ptr.go; then
59
59
exit 1
60
60
fi
61
61
62
+ # The malloc.go test should crash.
63
+ rm -f malloc.out
64
+ if go run malloc.go > malloc.out 2>&1 ; then
65
+ echo " ` go run malloc.go` succeeded unexpectedly"
66
+ cat malloc.out
67
+ rm -f malloc.out
68
+ exit 1
69
+ fi
70
+ rm -f malloc.out
71
+
62
72
rm -rf errs _obj
63
73
exit 0
Original file line number Diff line number Diff line change @@ -1463,9 +1463,15 @@ const cMallocDefGo = `
1463
1463
var __cgofn__cgoPREFIX_Cfunc__Cmalloc byte
1464
1464
var _cgoPREFIX_Cfunc__Cmalloc = unsafe.Pointer(&__cgofn__cgoPREFIX_Cfunc__Cmalloc)
1465
1465
1466
+ //go:linkname runtime_throw runtime.throw
1467
+ func runtime_throw(string)
1468
+
1466
1469
//go:cgo_unsafe_args
1467
1470
func _cgo_cmalloc(p0 uint64) (r1 unsafe.Pointer) {
1468
1471
_cgo_runtime_cgocall(_cgoPREFIX_Cfunc__Cmalloc, uintptr(unsafe.Pointer(&p0)))
1472
+ if r1 == nil {
1473
+ runtime_throw("runtime: C malloc failed")
1474
+ }
1469
1475
return
1470
1476
}
1471
1477
`
You can’t perform that action at this time.
0 commit comments