History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: RSRP-40810
Type: New Feature New Feature
Status: Open Open
Priority: Normal Normal
Assignee: Eugene Pasynkov
Reporter: Erwin Derksen
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
ReSharper

Cnontrol flow analysis: special checks for ThreadAbortException and Thread.ResetAbort invocation

Created: 18 May 07 15:50   Updated: 19 May 07 22:43
Component/s: Code Analysis - Control Flow, Code Analysis
Fix Version/s: Future Versions
Security Level: Everybody (All jira users)

Original Estimate: Unknown Remaining Estimate: Unknown Time Spent: Unknown

Build: 376


 Description  « Hide
In the code behind of an aspx page, I have the following code:
Module module = ...;
	if (module == null)
	{
		Response.Redirect("~/IncorrectRequestString.aspx");
	}
	Contract contract = module.Contract;

This code is giving me a "Possible NullReferenceException" on the last line of the example.
However, this is not possible as a Response.Redirect will throw a ThreadAbortException, as will the overload

Response.Redirect("~/IncorrectRequestString.aspx", true);

(See the help on this method (HttpResponse.Redirect).)

Could you please add these checks to your NullReferenceException analysis?



 All   Comments   Work Log   Change History      Sort Order:
Ilya Ryzhenkov - 18 May 07 16:01
In ReSharper options, on the value analysis page you can add "terminating methods". I.e. methods that never returns in the sense of code flow analysis. You could add Redirect methods to avoid false NullReferenceException warning.

On the other hand, if it throws exception one could catch it and use module once again, so we may need a flag on terminating methods, saying if method just never returns or throws unconditionally.


Erwin Derksen - 18 May 07 19:32
I didn't know about that option. So I could indeed use that.

However, a ThreadAbortException is a special exception as it can NOT be caught. So the last line in this example will NEVER be executed! In that sense, this method is a special case which could be handled by your analysis tool as a throw statement, without any user configuration necessary.

try
{
	Response.Redirect("~/IncorrectRequestString.aspx");
}
catch (ThreadAbortException)
{
	// statements here will execute, but exception will ALWAYS be rethrown
}
// execution will NEVER reach this point!

Eugene Pasynkov - 18 May 07 19:38
Actually, ThreadAbortException is re-throwed at every "catch (ThreadAbortException)" block until Thread.ResetAbort() is called

This logic is too specific and too complex to implement it in 3.0


Vladimir Reshetnikov - 19 May 07 22:43
> Actually, ThreadAbortException is re-throwed at every "catch (ThreadAbortException)" block

It is not quite correct. It is rethrown only if it is caused by Thread.Abort method, but not if it is "manually" thrown.

using System;
using System.Threading;

class Program
{
    static void Main()
    {
        ThreadAbortException e1 = null;
        try
        {
            Thread.CurrentThread.Abort();
        }
        catch(ThreadAbortException e2)
        {
            e1 = e2; // This is just to obtain an instance of ThreadAbortException, it has not public ctors
            Thread.ResetAbort();
        }

        try
        {
            throw e1; // Throw instance of ThreadAbortException "manually"
        }
        catch(ThreadAbortException) {}
        Console.WriteLine("It is not re-thrown!");
    }
}