-
Notifications
You must be signed in to change notification settings - Fork 18k
x/sys/windows: LocalAlloc should return an unsafe.Pointer or *byte #44836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
AFAIK the stdlib does not return unsafe.Pointer outside the unsafe package because it shouldn't be possible to get an unsafe.Pointer without importing the unsafe package. Perhaps the API could return windows.Pointer instead? Then you could convert it to with unsafe yourself. |
Ah, good point. Yeah, a |
AFAIK that’s not really an issue anymore, see #40592 (comment) and previous comments. |
cc @alexbrainman @zx2c4 |
@tmthrgd oh yeah, that’s true. I’d forgotten about that issue. (It’s an unfortunate choice imo, but oh well.)
… Le 6 mars 2021 à 22:12, Tom Thorogood ***@***.***> a écrit :
AFAIK that’s not really an issue anymore, see #40592 (comment) and previous comments.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
I'm fine with either, honestly. No matter what, if you have Windows allocate memory for you, you're going to unsafely cast it to whatever you need. I'd just like to be able to do that without tripping pointer unsafety warnings. |
I'll fix. |
Change https://golang.org/cl/299492 mentions this issue: |
I just tried to run
From what I know, if you build your program or test with -race flag, and it runs without any crashes, then your code is safe. I don't know enough about garbage collector and unsafe rules (and they change) to judge if Leaving for unsafe experts @ianlancetaylor and @mdempsky to decide, Thank you. Alex |
Yeah, any conversion from a raw uintptr to an unsafe.Pointer will anger I'm not sure how to adjust the syscall definitions to not use the flagged pattern. Arguably it's fine to just say "go vet is imprecise, it's okay to ignore warnings if you're sure"... But it'd be nice to avoid the false positives if there's an easy API tweak that can make it happen. |
Agree.
Changing external API will break existing users. Perhaps it is OK price to pay in this situation. Perhaps there is better solution here. And we should also fix other So I would like to hear from others before changing API. Alex |
For others who visit this issue later: just in case this doesn't get fixed somehow, you can use the following to trick
|
If these are truly false positives, one option would be to suppress this warning in |
False positive or not, https://golang.org/cl/299492 provides a much nicer API anyway. So let's just get this fixed up. |
I've abandoned my CL, and probably this issue can be closed. Doesn't seem like this is worth fixing. Also, LocalAlloc can return non-pointer handles depending on its arguments... |
Yeah, fair 'nuf. The API that can return handles or pointers makes it a bit strange anyway, and it's easy to get around the vet check. Thanks for considering! |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
N/A, bug report is for x/sys/windows
What did you do?
I'm writing a library to manipulate the Windows firewall at inet.af/wf. As part of calling the API, I need to construct a tree of C structs with various pointer unsafeties within. I'm using
windows.LocalAlloc
andwindows.LocalFree
to allocate memory on the non-go-managed heap.Unfortunately though,
windows.LocalAlloc
returns the pointer as a uintptr - presumably becauseLocalAlloc
can return either a raw pointer or a Windows handle to movable memory. This means that any time I want to use the allocated memory, I have to do an unsafe conversion that makesgo vet
mad:Even though these are famous last words, I believe that this unsafe conversion is actually safe, because the uintptr isn't representing a pointer into the Go heap. It would be nice to be able to use
LocalAlloc
without triggering a false positive ingo vet
.The two ways I can think of to achieve this are:
LocalAlloc
return anunsafe.Pointer
instead of auintptr
, to express that it can be cast to whatever pointer type you wish, subject to unsafety rules. That includes converting it to a uintptr, which is the underlying type of windows.Handle, for the case where you allocate a movable chunk of memory.LocalAlloc
into two functions, one returning an unsafe.Pointer for fixed allocs, and one returning a windows.Handle for movable allocs. That would make it easier to use movable allocs in Go (although I cannot think why you'd want to, so my preference would be for the prior option).The text was updated successfully, but these errors were encountered: