paypal / gatt

Gatt is a Go package for building Bluetooth Low Energy peripherals
BSD 3-Clause "New" or "Revised" License
1.14k stars 285 forks source link

Ioctl should take an unsafe.Pointer, not a uintptr #116

Open bazile-clyde opened 1 year ago

bazile-clyde commented 1 year ago

From the unsafe.Pointer rule (4) Conversion of a Pointer to a uintptr when calling syscall.Syscall.

If a pointer argument must be converted to uintptr for use as an argument, that conversion must appear in the call expression itself:

syscall.Syscall(SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(p)), uintptr(n))

The compiler handles a Pointer converted to a uintptr in the argument list of a call to a function implemented in assembly by arranging that the referenced allocated object, if any, is retained and not moved until the call completes, even though from the types alone it would appear that the object is no longer needed during the call.

The wrapper Ioctl violates this rule by requiring uintptrs in their arguments and later passing those to unix.Syscall. This will allow the object at the address the uintptr points to to be garbage collected. This could result in a random address being written to. A similar issue is being working in Go's ptrace function. See https://go-review.googlesource.com/c/go/+/470299. Ioctl should instead take unsafe.Pointers