Skip to content
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

Replace NullReferenceException thrown when interceptors swallow exceptions and cause a null value type to be returned #364

Merged
merged 1 commit into from
May 30, 2018

Conversation

jonorossi
Copy link
Member

See #85.

@stakx
Copy link
Member

stakx commented May 30, 2018

I haven't done a detailed code review but run a few quick tests against this PR. It appears to be doing what it should. 👍

I found one minor issue regarding ref parameters. Say you have this:

using Castle.DynamicProxy;
using System;

class Program
{
    static void Main()
    {
		var generator = new ProxyGenerator();

		var proxy = generator.CreateInterfaceProxyWithoutTarget<IFoo>(new Interceptor());

		int x = 0;
		proxy.GetValue(ref x);
    }
}

class Interceptor : IInterceptor
{
	public void Intercept(IInvocation invocation)
	{
		invocation.Arguments[0] = null;
	}
}

public interface IFoo
{
	void GetValue(ref int x);
}

Then you get an error mentioning x as being an out parameter:

Unhandled Exception: System.InvalidOperationException: Interceptors either trashed or failed to set a value for out parameter 'x'
   at Castle.Proxies.IFooProxy.GetValue(Int32& x)
   at Program.Main()

In general, I am not sure if the additional check is needed at all for ref (or in) parameters, since an interceptor would have to maliciously reset an argument to null to provoke this error.

@jonorossi
Copy link
Member Author

In general, I am not sure if the additional check is needed at all for ref (or in) parameters, since an interceptor would have to maliciously reset an argument to null to provoke this error.

I was wondering that too, it is probably unlikely someone would set an argument to null and wonder why it doesn't work. So remove the parameter check, and keep the return value check?

@jonorossi
Copy link
Member Author

@stakx do you think the exception message is clear enough, or could be worded better/differently?

@stakx
Copy link
Member

stakx commented May 30, 2018

So remove the parameter check, and keep the return value check?

The argument check is probably a good idea for out parameters. So I'd say, only remove the parameter check for by-ref, but non-out parameters.

@jonorossi
Copy link
Member Author

The argument check is probably a good idea for out parameters. So I'd say, only remove the parameter check for by-ref, but non-out parameters.

If you run the code you provided above with master you'll get a NullReferenceException because of the way all reference parameters work, they aren't actually "ref" because they can't be so their value is copied.

I can fix the exception to mention "ref" rather than "out", that was my mistake.

@stakx
Copy link
Member

stakx commented May 30, 2018

Interceptors either trashed or failed to set a return value; or swallowed the thrown exception

do you think the exception message is clear enough, or could be worded better/differently?

Generally, as a hint at the possible error source, it's definitely clear enough. The following points are just minor fine-tuning suggestions:

  • As a non-native English speaker, I did initially stumble over the word "trashed"... what kind of error situation does this cover? I'm not sure if I'm missing something here, but the error message could possibly be simplified to just Interceptors failed to set a return value; .... (Why the interceptors failed to do that is probably not too important.)

  • Perhaps add a brief explanation where the swallowed exception originated: ...; or swallowed the exception thrown by the target. (Do such swallowed exceptions in fact always originate from targets invoked by invocation.Proceed()? Or are there scenarios where this problem occurs when you have a chain of several interceptors?)

@stakx
Copy link
Member

stakx commented May 30, 2018

So remove the parameter check, and keep the return value check?

The argument check is probably a good idea for out parameters. So I'd say, only remove the parameter check for by-ref, but non-out parameters.

I need to take this back, I didn't realise that out parameter appear to be zero-initialized anyway, so the error would only occur when the interceptor makes an effort to set it to null explicitly.

So yes, perhaps the check could be removed completely for all by-ref parameters (even out ones).

@jonorossi jonorossi force-pushed the interceptor-returning-null-value-type branch from 5991c32 to b5cd585 Compare May 30, 2018 17:20
@jonorossi
Copy link
Member Author

Yep, your suggestions for the exception message make sense, done. And removed the parameter check.

Copy link
Member

@stakx stakx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Seems a bit silly to me that I dare "approve" the project owner's own PR, but well... it's GitHub's wording. :)

Looks good to me! 👍

…tions and cause a null value type to be returned
@jonorossi jonorossi force-pushed the interceptor-returning-null-value-type branch from b5cd585 to d75e5b3 Compare May 30, 2018 17:30
@jonorossi jonorossi merged commit fd0a0e8 into master May 30, 2018
@jonorossi jonorossi deleted the interceptor-returning-null-value-type branch May 30, 2018 17:38
@jonorossi jonorossi added this to the vNext milestone May 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants