-
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
bytes: Buffer should add copy check #48398
Comments
Duplicate of #25907 |
This is not duplicate of #25907, strings.Builder do dynamic copy check, but bytes.Buffer is leak of dynamic copy check. |
I agree that this is not quite a dup of #25907. But I don't think we should add a dynamic copy check here. For |
Not everyone who using golang knows the internal implementation of bytes.Buffer, so we should not think the "unexpected result" is common sense. If this is not a duplicate of #25907, I think we should reopen this issue, instead of closing it roughly. |
OK, I will reopen it, but I expect the result will be to close it gently. Copying a |
bytes.Buffer already existed for 12 years, add a copy check will break some working codes indeed. // A Buffer is a variable-sized buffer of bytes with Read and Write methods.
// The zero value for Buffer is an empty buffer ready to use.
// Do not copy a non-zero Buffer.
type Buffer struct {
} |
I would argue that we should. You don't need to know Buffer's internals to know that in Go, it is generally a bad idea to copy a value of type T that has methods associated with *T, because method calls may lead to subtle aliasing relationships. For example, a call may cause one field of a struct to refer to another field. Go's direct copy semantics are not appropriate for such values, because they cause both the copy and the original to refer to the original. You need to call a Clone method, if the type provides one, or avoid copying altogether. |
We all know the consequence of copy a non-zero value of bytes.Buffer, but there is no warning or mechanism to prevent it. We don't want golang users need to know those tricks before writing their codes. Your example is correct, but bytes.Buffer is in standard library, it should be out-of-the-box. I think that's the reason why strings.Builder have dynamic copy check. |
What version of Go are you using (
go version
)?What did you do?
What did you expect to see?
Provide copy check to prevent value copy of bytes.Buffer.
What did you see instead?
a influence by b
The text was updated successfully, but these errors were encountered: