Skip to content

[GeneratedComInterface] Generates incorrect code if base interface has unsafe method #120388

@jevansaks

Description

@jevansaks

Description

If a base type has an unsafe method then GeneratedComInterface generates methods for the derived class that need unsafe but are missing the keyword:

    public partial interface IDerivedInterface
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.ComInterfaceGenerator", "9.0.12.41916")]
        [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute]
        new void BaseMethod(void* pBuffer) => ((global::Repro.IBaseInterface)this).BaseMethod(pBuffer);
    }

Error: Pointers and fixed size buffers may only be used in an unsafe context

Practical scenario where this comes up: ISequentialStream defines Read/Write that take pointers and IStream derives from it.

I also tried net10.0 (10.0.100-rc.1.25451.107 ) and it still reproed.

Reproduction Steps

Put this code in a new console app (I also put the repro here: https://github.com/jevansaks/GeneratedComInterfaceRepro/blob/master/Program.cs)

    [Guid("00000000-0000-0000-0000-000000000011"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), GeneratedComInterface]
    public partial interface IBaseInterface
    {
        unsafe void BaseMethod(void* pBuffer);
    }

    [Guid("00000000-0000-0000-0000-000000000012"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), GeneratedComInterface]
    public partial interface IDerivedInterface : IBaseInterface
    {
        void DerivedMethod();
    }

Expected behavior

GeneratedComInterface needs to add unsafe

Actual behavior

No unsafe keyword.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

Status

No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions