-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Avoid Unsafe.As
in BitConverter
#112616
Avoid Unsafe.As
in BitConverter
#112616
Conversation
Tagging subscribers to this area: @dotnet/area-system-numerics |
Why is this better? |
We should really just use |
It doesn't depend upon implementation specific behaviour, i.e. that the array data is properly aligned. |
Is this something that can be improved? Might be worth opening an issue :) |
In the future, please include your reasoning for the proposed changes in the PR description. Thanks. |
Looks like a simple addressing mode was not contained. It's just that this PR replaces one unsafe code with another unsafe (a tiny bit better, but still). |
@jakobbotsch do we have an issue tracking this? |
I don't think we do. Feel free to open one. |
Unsafe.As
in BitConvertor
Unsafe.As
in BitConverter
Thanks! The diffs look sane considering these APIs allocate (and tracked by #104538). I have a general question to @stephentoub @jkotas So in this case BinaryPrimitives.WriteInt32LittleEndian
BinaryPrimitives.WriteInt32BigEndian Do we want to add new APIs there which check the endianess inside? +BinaryPrimitives.WriteInt32 So then for any perfectly safe Read/Write for primitives we can use |
What's the rationale for why |
I think mainly to protect from cases where structs with paddings/non-blittable fields (like bool) are used as T (probably can be protected by some theoretical |
Before we start talking about introducing new APIs, we should agree on the definition. If we decide that such functionality needs to be considered "unsafe", and if the relevant APIs are considered important enough, and if there isn't an existing counterpart, we could certainly introduce additional methods. But there are several ifs there. I'm also not sure how such a |
Good point!
TBH, it's a bit confusing we have these primitives spread in BitConverter, BinaryPrimitives, BitOperations, MemoryMarshal 🙂 |
If the bar for
Can this change use |
@xtqqczze could you please try that? |
src/libraries/System.Private.CoreLib/src/System/BitConverter.cs
Outdated
Show resolved
Hide resolved
Diffs are as expected with this approach |
@xtqqczze Thanks! |
Doesn't depend upon implementation specific behaviour, i.e. that the array data is properly aligned.