Project
ReSharper
Priority
Normal
Type
Feature
Fix versions
Mirabile Futurum
State
Open
Assignee
Evgeny Pasynkov
Subsystem
Code Analysis
Affected versions
No Affected versions
Fixed in build
No Fixed in build
  • Created by   Erwin Derksen
    4 years ago (18 May 2007 16:50)
  • Updated by   Erwin Derksen
    4 years ago (19 May 2007 23:43)
  • Jira: RSRP-40810
    (history, comments)
 
RSRP-40810 Cnontrol flow analysis: special checks for ThreadAbortException and Thread.ResetAbort invocation
0
Issue is visible to: All Users
  The issue is visible to the selected user group only
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?
Comments (4)
 
History
 
Linked Issues (?)
 
TeamCity Changes (0)
 
Ilya Ryzhenkov
  Ilya Ryzhenkov
18 May 2007 17:01
4 years ago
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
  Erwin Derksen
18 May 2007 20:32
4 years ago
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!
Evgeny Pasynkov
  Evgeny Pasynkov
18 May 2007 20:38
4 years ago
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
  Vladimir Reshetnikov
19 May 2007 23:43
4 years ago
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!");
    }
}