-
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
x/exp/slices: memory leak on Delete #54650
Comments
Change https://go.dev/cl/425209 mentions this issue: |
Change https://go.dev/cl/425209 mentions this issue: |
Change https://go.dev/cl/425894 mentions this issue: |
Change https://go.dev/cl/425895 mentions this issue: |
Now doing an explicit bounds check, to panic on invalid indices Fixes part (3) of golang/go#54650 Change-Id: I62a70c0f866e6c336d0c7feae5af33272fc568b6 Reviewed-on: https://go-review.googlesource.com/c/exp/+/425894 Auto-Submit: Keith Randall <khr@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Keith Randall <khr@golang.org>
Mitigates golang/go#54650 Change-Id: I899094ec6af8d3b1985e737a5318e9776b4cb1a9 Reviewed-on: https://go-review.googlesource.com/c/exp/+/425895 Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
Current status:
|
@Deleplace I would think that in stead of documenting the memory leak, it should be fixed. Otherwise people will have to implement their own safer Delete. |
Thanks for your work on this. We aren't going to change |
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?
The current implementation of slices.Delete is very straightforward but it has a number of drawbacks:
I suggest fixing this by explicitly zeroing the tail of the original slice, and by explicitly checking that i<=j.
The problematic behaviors and suggested fix are shown at https://go.dev/play/p/ZhX_LRGm9N1
The memory leak (1) doesn't exist for non-pointer value types.
Zeroing the right tail fixes (1) for pointer types and changes the values visible from the original slice header. This is okay because Delete is a destructive function and makes no promise to keep the extra capacity unchanged.
The documentation says "Delete panics if s[i:j] is not a valid slice of s." which is currently not true because s[2:1] is not a valid slice (see https://go.dev/play/p/ftvpm65GGzL) but instead of panicking, Delete produces non-sensical results (see https://go.dev/play/p/sIJRru2sGPa). The new suggested implementation fixes this bug.
I will send a change containing the suggested implementation, and extra test cases.
The text was updated successfully, but these errors were encountered: