aboutsummaryrefslogtreecommitdiff
path: root/src/unsafe
diff options
context:
space:
mode:
authorKeith Randall <khr@google.com>2018-10-30 13:30:09 -0700
committerKeith Randall <khr@golang.org>2018-11-14 21:01:36 +0000
commit0c7762cd184649552309c82671bf81f89d215ff7 (patch)
treedc33c01dad6596dd41f849a74fdccdea415a7e46 /src/unsafe
parenta063a2284af69d25d4367ff3e7c6da53f37bad03 (diff)
downloadgo-0c7762cd184649552309c82671bf81f89d215ff7.tar.gz
go-0c7762cd184649552309c82671bf81f89d215ff7.zip
cmd/compile: assume unsafe pointer arithmetic generates non-nil results
I've never seen a case where unsafe arithmetic is used to generate a nil. (Something like var x uintptr; unsafe.Pointer(x - x).) We can assume that if someone is doing arithmetic with pointers, the result will be non-nil. Our unsafe rules already forbid this, although we should be more explicit. RELNOTE=It is invalid to convert a nil unsafe.Pointer to uintptr and back, with arithmetic. (This was already invalid, but this statement has been added for clarification.) Fixes #27180 Change-Id: I1880b7725a9fd99e4613799930fdad9aaa99e8f0 Reviewed-on: https://go-review.googlesource.com/c/146058 Reviewed-by: Austin Clements <austin@google.com>
Diffstat (limited to 'src/unsafe')
-rw-r--r--src/unsafe/unsafe.go6
1 files changed, 6 insertions, 0 deletions
diff --git a/src/unsafe/unsafe.go b/src/unsafe/unsafe.go
index e16c4aeacb..272761d936 100644
--- a/src/unsafe/unsafe.go
+++ b/src/unsafe/unsafe.go
@@ -99,6 +99,12 @@ type ArbitraryType int
// u := uintptr(p)
// p = unsafe.Pointer(u + offset)
//
+// Note that the pointer must point into an allocated object, so it may not be nil.
+//
+// // INVALID: conversion of nil pointer
+// u := unsafe.Pointer(nil)
+// p := unsafe.Pointer(uintptr(u) + offset)
+//
// (4) Conversion of a Pointer to a uintptr when calling syscall.Syscall.
//
// The Syscall functions in package syscall pass their uintptr arguments directly