Skip to content

Remove a number of calls to Unsafe.AsPointer #99144

Open
@hamarb123

Description

@hamarb123

There are a number of unnecessary uses of Unsafe.AsPointer in this repository (e.g.

_EnumerateConfigurationValues(Unsafe.AsPointer(ref context), &ConfigCallback);
) - this is a extra dangerous API that can easily be used wrongly (see #99138 as an example that slipped by a number of people). While the current remaining uses seem safe currently at a glance, we might be better off replacing a number of the unnecessary uses with other APIs, and/or adding a comment explaining why it's safe in this case (like in
(byte*)Unsafe.AsPointer(ref _blocks[0]), // This is safe to do since we are a ref struct
), and disrecommending their use in the future without a very good reason.

Most of the uses can be broken in to 2 categories:

The idea was mentioned by @tannergooding on the discord, and one of the suggested solutions was to create an internal API bool IsOpportunisticallyAligned(ref T address, nuint aligment) or similar for the alignment uses.

I am happy to make PRs for these. @tannergooding suggested that I make one for each area separately. Just making this issue to track it and get support for the general idea from people.

/cc @stephentoub @jkotas @tannergooding

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions