-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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: TrimXXX makes it easy to unintentionally overwrite slice #43341
Comments
If this issue needs to be fixed then I'd like to send a CL for this if it's fine. |
This comment has been minimized.
This comment has been minimized.
@seankhliao I know how slices work. Look at CL for #30169! |
I see, after reading the linked issues, 👍 |
It would help to see the kind of analysis that was done in #21149 (comment). My intuition is that |
Yeah, right. Okay, I'll do some research on this! |
Just to give an extra data point - I've used Trim funcs many times with the knowledge that they do not allocate. Changing that would be surprising to me, and would make perfectly valid code suddenly allocate more. That could very well mean regressions if the call is in performance-sensitive code. |
If the syntax
to always make an allocation. |
I'm not sure if isolated examples are helpful (it's fairly easy to find examples that support either semantic). What would be needed is a thorough analysis that answers something like:
Currently, based on the 👍 on comments above, it seems that people generally prefer the current semantic. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Trim
and friends (TrimXXX
) in thebytes
package seem to unintentionally overwrite slices. Well, this could be working as designed but do refer:So, based on CLs that fixed the issues listed above, I think it'd be appropriate to match the capacity to the length so that it could be avoided and match the behaviour of other functions.
Link to the playground: https://play.golang.org/p/yGJxI_Jnkk6
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: