-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: reflect/v2: remove Value.Bytes #27727
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
Comments
The current |
Is there a |
Updated proposal to remove references to the nonexistent |
To avoid breaking compatibility, maybe let the |
Aha, sorry, then it still breaks the compatibility. |
How would that work, given that it's always possible to access the
I suppose that
That won't work if the slice is a named type with underlying type The workaround is possible, but a bit more awkward and with some performance implications. https://play.golang.org/p/JWXNzh238Ju |
Good question! That could happen at the point where the |
I'm not sure I can see how that could work. AFAICS it means that
I suspect that in this particular case, doing a Go 1/2 split is quite hard. I wonder: would it be so bad if |
Background
As noted in #24746 (comment),
(reflect.Value).Bytes
is the onlyreflect.Value
methods that return mutable values without enforcingCanSet
orCanInterface
.That makes dangerous mutations through
[]byte
(and slices of defined types withbyte
as the underlying type) more difficult to detect, since they do not involveunsafe
orsyscall
in the way that other dangerous mutations must.This is a proposal to close that mutability loophole and make the handling of
byte
slice types more consistent with other slices.Proposal
This proposal consists of two parts:
Value.Bytes
, making it accessible only to Go 1 code.reflect.Copy
,reflect.Append
, andreflect.AppendSlice
to allow the source argument (but not the destination) to be obtained through unexported fields, provided that the element type of the slice does not contain any pointers or interfaces.Users could replace a call to
Bytes
on an exported field of known element type with a call toInterface
and a type-assertion (https://play.golang.org/p/9LJIDW7ftZx).Convert
call (https://play.golang.org/p/gMGApEvicM-).To examine slices obtained via unexported fields (and slices of defined types with
byte
or as their underlying type), users could employ any of four strategies:unsafe
to alias the contents of the slice to a slice variable (https://play.golang.org/p/LZTiDZz1FAl).Note that the latter three are already possible today: part (2) of this proposal only affects the clarity of the code, not what is possible to express.
Alternatives
We could keep the
Bytes
method, but make it panic ifCanInterface
returns false.unsafe
operation.We could go even further and remove
SetBytes
, since it is redundant withCopy
.[]byte
more like every other slice type.If we had read-only slices (#22876 and associated issues), we could make
Bytes
return a read-only slice.The text was updated successfully, but these errors were encountered: