Skip to content

Conversation

@dangodangodango
Copy link

According to: https://pkg.go.dev/unsafe#Pointer Unsafe.Pointer rule 4:

For the compiler to recognize this pattern, the conversion must appear in the argument list:

// INVALID: uintptr cannot be stored in variable
// before implicit conversion back to Pointer during system call.
u := uintptr(unsafe.Pointer(p))
syscall.Syscall(SYS_READ, uintptr(fd), u, uintptr(n))

Storing a go pointer to uintptr variable is dangerous, the address of the go variable that the uintptr pointers to may change (stack grow), or the variable be garbage-collected(uintptr doesn't ref the value).
Even worse, the original address may be reused by another goroutine, and if the syscall writes to that address, it's can cause memory corruption and random runtime errors.

@dangodangodango dangodangodango requested a review from a team as a code owner October 31, 2025 17:34
@dangodangodango
Copy link
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant