-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
cmd/cgo: document that code should only examine errno in case of error #63485
Comments
I don't see how we can change the existing cgo behavior. Existing code may be depending on it. And, as you say, there is no reasonable way that cgo can determine whether an arbitrary C function has succeeded or not. So I think that all we can do here is documentation. Taking this out of the proposal process. In general the cgo approach follows C language rules: only check the error if the function fails. In C the error is checked by looking at the |
That would be weird. The current cgo behavior is to return
I know. My whole point is that go developers expect go language rules, rather than C language rules. I believe the job of a bridge like cgo is precisely to translate the source behavior (in this case, C) into the target behavior (in this case, go). This being said, while I had a slight hope that someone with a better C knowledge than myself would provide a generic mean to detect whether a function failed, as far as I know this is just not possible. So no matter what we think cgo should do, documenting the current behavior is probably our only option. |
@EtienneMiret @ianlancetaylor can you add label of pkgsite |
Change https://go.dev/cl/616264 mentions this issue: |
The cgo documentation states:
The problem here is that the C
errno
variable may be set even when there is no error (man 3 errno
is very clear about this) but go developers expect the error return value to benil
when there is no error.I know google/gopacket#147 was caused by this invalid expectation. I guess there are other bugs laying around for this same reason. When investigating that bug, I decided to open a PR in gopacket because cgo was behaving as specified.
Thinking again about it, I’m now wondering whether the cgo spec is how we want it.
An obvious issue is that it will be hard for cgo to know whether a C function returned an error (in which case
errno
should be read and translated into a go error) or was successful (in which case we would like the go error to benil
no matter what theerrno
value is). I believe C functions usually report an error by returning -1 or NULL. But as far as I know, it is the caller’s responsibility to check the specific manual for the function they call, see how it reports error and take appropriate action. Something a generic tool like cgo cannot do.So maybe the only thing that can be done about this wrong expectation is to warn go developers against it in the cgo documentation. In any case, I believe it is worth opening this discussion.
The text was updated successfully, but these errors were encountered: