-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: C.GoStringN should document that it copies the entire length of the buffer #12427
Comments
One way to understand is that Go string can have embedded NULs, I don't think changing the type of the first argument to C.GoString/ |
When you say that something takes a *C.char as its argument and describe that as a C string, you are creating the expectation in many readers that it will actually be treated as a C string. C strings are null terminated (and so by definition do not contain nulls). Making an argument an unsafe.Pointer (and not calling it a C string in documentation) makes it clear that it is not being treated as a C string and so nulls do not matter. Contrast the lack of ambiguity in C.GoBytes versus the current ambiguity in C.GoStringN. I agree that C.GoStringN serves a useful purpose today with its current semantics and anyway, the current behaviour is what it is and people have written code to that behaviour. I just want those semantics to be clear to everyone who reads |
No, char * in C doesn't necessary represent a C string. I don't think taking a *C.char parameter alone implies And in other languages that supports embedded NULs e.g. CPython: |
It seems fine to me to change:
to say
|
CL https://golang.org/cl/15997 mentions this issue. |
A naive reader of the minimal documentation for C.GoStringN() would be led to believe that it is a bounded version of C.GoString(), ie it copies a null-terminated string but stops after at most N bytes whether or not a null was found. This is not the actual behaviour, which is that GoStringN() always copies N bytes regardless of whether or not the 'C string' is null terminated before then. Since I recently made this mistake myself, I think that the cmd/cgo documentation should be clear about this.
(In an ideal world I think that C.GoStringN()'s first argument would be an unsafe.Pointer instead of a *C.char, but I assume it's far too late to make that change even if people were inclined to do so.)
The text was updated successfully, but these errors were encountered: