mirror of https://github.com/golang/go.git
cmd/cgo: throw if C.malloc returns nil
Change-Id: If7740ac7b6c4190db5a1ab4100d12cf16dc79c84 Reviewed-on: https://go-review.googlesource.com/31768 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
parent
643c6b3c74
commit
b4ce38ec57
|
|
@ -0,0 +1,23 @@
|
||||||
|
// 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"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
p := C.malloc(C.size_t(^uintptr(0)))
|
||||||
|
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.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -59,5 +59,15 @@ if ! go run ptr.go; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# The malloc.go test should crash.
|
||||||
|
rm -f malloc.out
|
||||||
|
if go run malloc.go >malloc.out 2>&1; then
|
||||||
|
echo "`go run malloc.go` succeeded unexpectedly"
|
||||||
|
cat malloc.out
|
||||||
|
rm -f malloc.out
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
rm -f malloc.out
|
||||||
|
|
||||||
rm -rf errs _obj
|
rm -rf errs _obj
|
||||||
exit 0
|
exit 0
|
||||||
|
|
|
||||||
|
|
@ -1463,9 +1463,15 @@ const cMallocDefGo = `
|
||||||
var __cgofn__cgoPREFIX_Cfunc__Cmalloc byte
|
var __cgofn__cgoPREFIX_Cfunc__Cmalloc byte
|
||||||
var _cgoPREFIX_Cfunc__Cmalloc = unsafe.Pointer(&__cgofn__cgoPREFIX_Cfunc__Cmalloc)
|
var _cgoPREFIX_Cfunc__Cmalloc = unsafe.Pointer(&__cgofn__cgoPREFIX_Cfunc__Cmalloc)
|
||||||
|
|
||||||
|
//go:linkname runtime_throw runtime.throw
|
||||||
|
func runtime_throw(string)
|
||||||
|
|
||||||
//go:cgo_unsafe_args
|
//go:cgo_unsafe_args
|
||||||
func _cgo_cmalloc(p0 uint64) (r1 unsafe.Pointer) {
|
func _cgo_cmalloc(p0 uint64) (r1 unsafe.Pointer) {
|
||||||
_cgo_runtime_cgocall(_cgoPREFIX_Cfunc__Cmalloc, uintptr(unsafe.Pointer(&p0)))
|
_cgo_runtime_cgocall(_cgoPREFIX_Cfunc__Cmalloc, uintptr(unsafe.Pointer(&p0)))
|
||||||
|
if r1 == nil {
|
||||||
|
runtime_throw("runtime: C malloc failed")
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue