malloc.go 766 B

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