-
Notifications
You must be signed in to change notification settings - Fork 18k
reflect: Bytes allows conversion of invariant slices #24746
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
cool finding.
|
A little related: #23536 |
Very related: #19778. |
Change https://golang.org/cl/105937 mentions this issue: |
I'm not sure this is a real problem. It would be a minor problem if the reflect package let you convert a |
Interesting or not, usages of As an example of bad usages, the |
Sure, I'm not surprised that people rely on it. What I'm trying to say is that I think it's OK to permit this kind of type violation. It's not the kind of type violation that the reflect package is not supposed to permit. |
The hole doesn't do bad, pls don't fix it. :) |
One surprising side-effect it is that it allows the program to bypass the usual restrictions on setting unexported fields (and fields of unexported types), without even importing |
Yep, it's a risky method all around. |
This is why this hole is so useful. |
This type of sentiment is why this hole is dangerous. Needing to import |
I don't see any dangerousness here. I don't think Go type system forbids this conversion for security reason. |
And if it is dangerous, the proposal, #19778, should have been rejected already now. |
The problem here is arguably not the conversion at all. Rather, it is that If you try to write the equivalent code by copying the slice value out to a different variable, you get #19778 is not relevant to this problem because ordinary Go code enforces unexported fields and types at compile-time instead of run-time. (It is already impossible to write these examples using ordinary Go code, because outside packages cannot name the types and/or fields involved.) |
See the numerous Playground examples above. It's not “dangerous” in the “undefined behavior” sense, but rather in the “allows a program to violate API restrictions” sense. |
Read-only slices (#22876) could address this problem: they would allow the Programs that need to obtain a read-write slice can already do so today, subject to the usual restrictions on unexported fields and types, by calling |
@bcmills Agree with you that your play examples are not "dangerous", |
fixed missing a NOT in the last comment. And I don't think this violation will affect the possible read-only slice feature later. |
I'm going to close this because I really don't think there is anything to be done. I suggest that you write a Go 2 proposal to remove the |
Done: #27727. |
(This is a very minor issue), but consider the following:
According to the Go specification,
Bytes1
andBytes2
are convertible to each other, butBytes3
is not convertible to any of the other since the underlying element type differs (even ifbyte
andMyByte
are convertible to each other). In other wordsBytes3
is not convertible to a[]byte
.The
Value.Bytes
is a convenience method that retrieves the[]byte
if convertible to such. However, this is possible:This allows us to use reflect to convert
Bytes3
toBytes1
, when we couldn't do so in the language.This applies to
Value.Bytes
,Value.Runes
,Value.SetBytes
, andValue.SetRunes
.The text was updated successfully, but these errors were encountered: