Skip to content

Castle allows to rewrite read-only parameters (in modifier) #369

@zvirja

Description

@zvirja

Consider the following code sample:

[Fact]
public void TestProxy()
{
    var proxy = new ProxyGenerator().CreateInterfaceProxyWithoutTarget<IInterfaceWithIn>(new RewritingInterceptor());
    
    var val = new MutableStruct { Value = 24 };
    proxy.DoNotMutate(val);

    Assert.Equal(24, val.Value);
}

public struct MutableStruct
{
    public int Value { get; set; }
}

public interface IInterfaceWithIn
{
    void DoNotMutate(in MutableStruct value);
}

public class RewritingInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        if (invocation.Method.Name == "DoNotMutate")
        {
            invocation.SetArgumentValue(0, new MutableStruct {Value = 42});
        }
    }
}

The issue happens because value is passed by ref, so technically it's possible to rewrite the value. However, like Roslyn, Castle should not allow to rewrite the value via API.

Likely, the fix should be placed somewhere here.

/cc: @stakx @blairconrad JFYI as our mock libraries contain tooling for this feature invocation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions