You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Crafted an example such that the memory referenced by the result overlaps with the insert-from slice, playground
a := []int{1, 2, 3, 4, 5, 6, 7, 8}
b := a[:4] // 1, 2, 3, 4
c := a[4:6] // 5, 6
res := slices.Insert(b, 2, c...)
fmt.Println(res)
What did you expect to see?
[1 2 5 6 3 4]
What did you see instead?
[1 2 3 4 3 4]
Note: This is because the implementation of slices.Insert slides the upper range of the first operand forward before copying the content of the third operand into the freed space. This risks overwrite the content in that operand. I believe slices.Replace suffers the same issue, too.
A conservative solution is to create a temporary buffer to copy the content of the second slice into before sliding the first one, while a more aggressive one would be to do pointer arithmetic to check the aliasness.
The text was updated successfully, but these errors were encountered:
Handle cases where the inserted slice is actually part of the slice
that is being inserted into.
Requires a bit more work, but no more allocations. (Compare to #494536.)
Not entirely sure this is worth the complication.
Fixesgolang#60138
Change-Id: Ia72c872b04309b99025e6ca5a4a326ebed2abb69
Reviewed-on: https://go-review.googlesource.com/c/go/+/494817
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
eric
pushed a commit
to fancybits/go
that referenced
this issue
Sep 7, 2023
Handle cases where the inserted slice is actually part of the slice
that is being inserted into.
Requires a bit more work, but no more allocations. (Compare to #494536.)
Not entirely sure this is worth the complication.
Fixesgolang#60138
Change-Id: Ia72c872b04309b99025e6ca5a4a326ebed2abb69
Reviewed-on: https://go-review.googlesource.com/c/go/+/494817
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
What did you do?
Crafted an example such that the memory referenced by the result overlaps with the insert-from slice, playground
What did you expect to see?
What did you see instead?
Note: This is because the implementation of
slices.Insert
slides the upper range of the first operand forward before copying the content of the third operand into the freed space. This risks overwrite the content in that operand. I believeslices.Replace
suffers the same issue, too.A conservative solution is to create a temporary buffer to copy the content of the second slice into before sliding the first one, while a more aggressive one would be to do pointer arithmetic to check the aliasness.
The text was updated successfully, but these errors were encountered: