ClangSharp icon indicating copy to clipboard operation
ClangSharp copied to clipboard

Missing Unsafe for Functions with Fixed Size Buffers

Open skrobchik opened this issue 10 months ago • 1 comments

The code generator adds unsafe to functions that use pointers, but not fixed size buffers. This results in error CS0214: Pointers and fixed size buffers may only be used in an unsafe context

See https://github.com/microsoft/wdkmetadata/issues/87 for an example.

Relevant code:

https://github.com/dotnet/ClangSharp/blob/4c866fbf76e46c3373d2df6093203acf08a376aa/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs#L6217

    private bool IsUnsafe(NamedDecl namedDecl, Type type)
    {
        var remappedName = GetRemappedTypeName(namedDecl, context: null, type, out _, skipUsing: true, ignoreTransparentStructsWhereRequired: false);
        return remappedName.Contains('*', StringComparison.Ordinal);
    }

skrobchik avatar Feb 26 '25 14:02 skrobchik

I think adding a comparison for [ or ] should be fine.

return remappedName.Contains('*', StringComparison.Ordinal) || remappedName.Contains(']', StringComparison.Ordinal);

skrobchik avatar Feb 26 '25 15:02 skrobchik