-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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
slices: Clone keeps source array alive when source slice is zero length #68488
Comments
Open to contribute with a CL in case this can be confirmed. |
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
See this modified version: https://go.dev/play/p/uSY4HsNVTjQ
As well as: Lines 348 to 349 in 90c6558
the behaviors are different. I'll submit a fix thx for the report. |
@Jorropo this also happens when the source slice is non-zero length. In
|
@diegommm the arrays don't overlap when using |
Change https://go.dev/cl/598875 mentions this issue: |
I'm not convinced that this is worth fixing. Finalizers don't make any promises. Calling |
I would expect that |
@ianlancetaylor Could you clarify what do you mean by "worth fixing"? Do you see any downside of applying this fix? |
On the CL @randall77 gave a good rationale for the change, and I'm OK with it. |
There is not a perfect clone implementation in Go. I agree that simplicity is not important for the implementation of the |
On the other hand, I think Go runtime should specially handle zero-capacity slicing results, along with taking addresses of zero-size values, to make the data pointers point to a global unique address within each program run. Doing this will remove many surprises in Go programming. [edit] Doing this will benefit the cases shown in this issue and in https://go.dev/play/p/ENRf3aUWhV2 and https://go.dev/play/p/kBhZIEk77Dz |
@zigo101 would it make sense instead to just use a nil pointer instead of using a global unique address for all zero-size values? |
In Go, |
I was asking this kind of question in golang-nuts since I didn't know this issue is already exist, thanks @randall77 for pointing me to this issue. There is already CL (https://go-review.googlesource.com/c/go/+/598875) for this, thanks for that. I just want to share a real world use case that I come across when I try implementing Garmin's FIT Protocol encoding in Go, FIT SDK for Go, in case this may add another rationale why the fix is matter. Here is the context: In a nutshell, FIT file is a binary file format that consist of multiple messages, and each message has fields:
Event though there is len(fields) "n", I can't simply use mesg.Fields := d.fieldsArray[:0] // big enough to hold all fields
d.decodeFields(&mesg)
for _, field := range mesg.Fields {
// d.expandComponent(mesg, field):
value := field.Value
for _, component := range field.Components {
// Expand until value is depleted.
if value == 0 {
break
}
compField := factory.CreateField(mesg.Num, component.Num)
mask := (1 << component.Bits) - 1
compField.Value = value & mask
value = value >> component.Bits
mesg.Fields = append(mesg.Fields, compField)
if len(compField.Components) > 0 {
d.expandComponent(mesg, compField) // Recursive
}
}
}
mesg.Fields = append(mesg.Fields[:0:0], mesg.Fields...) // slices.Clone Now, this is the related part of this issue, if by any chance And also, in my opinion, having |
Go version
go version go1.22.5 linux/amd64
Output of
go env
in your module/workspace:What did you do?
See this playground.
What did you see happen?
What did you expect to see?
The text was updated successfully, but these errors were encountered: