DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

I saw a little talk in the blogshpere about using String.IsNullOrEmpty in .NET 2.0, so I thought, just for fun I would run it through a series of tests.  Unfortunately in doing so I found a very serious bug with it.  The following C# code shows the problem:
classProgram
{
 
static void Main(string[] args)
{
   Console.WriteLine("starting");
   test(null);
   Console.WriteLine("finished");
   Console.ReadLine();
}
static void test(string x)
{
   for (int j = 0; j < 10; j++)
   {
      if (String.IsNullOrEmpty(x))
     {
        //TODO:
      }
   }
}
}
 
 
Have you spotted the problem ?  The astute of you are probably saying "yeh it's in that lame language C#", and although you'd be right, that's not the actual gotchya here.
 
The problem is when you compile this application as a release build with optimizations on and run it from outside of the IDE.  Note from inside the IDE all seems fine.  but when run form outside this will lead to a null exception being thrown at runtime.  Try it for yourselves !
 
Now in all fairness, this is actually a JIT optimization problem that impacts both VB and C#.  Vb.NET does provide a pretty good alternative syntax that does not have this issue, via the:
   If x = Nothing syntax. 
Of course in both VB and C# you could use the:
 If x Is Nothing OrElse x.Length= 0 Then
approach, but that seems to loose a bit in the readability.
 
So String.IsNullOrEmpty can cause unexpected runtime errors that you won't detect when running from inside the IDE.  I recommend using extreme CAUTION when using this method.
 
 
Published Tuesday, April 04, 2006 7:40 PM by bill
Filed under: ,

Comments

# WARNING: String.IsNullOrEmpty

Actuellement, tjs pour VB Code Snippet Editor, Bill essaye d'optimiser la compilation en C#, et il est...

Tuesday, April 04, 2006 9:36 AM by Richard Clark

# Watch out for those Nulls.

Bill McCarthy found a very weird bug today. It seems that if you craft some code just right and do a...

Tuesday, April 04, 2006 3:45 PM by Public Class GeoffAppleby

# .Net 2.0: String.IsNullOrEmpty causes runtime exception

Tuesday, April 04, 2006 4:50 PM by Anatoly Lubarsky

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Would you mind sharing exactly why this is happening.

Wednesday, April 05, 2006 12:40 AM by Ayende Rahien

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

The JIT team hasn't officially said yet. My guess is that the JIT is removing the check for null on the parameter being passed to String.IsNullOrEmpty as this is often superfulous

Wednesday, April 05, 2006 12:49 AM by bill

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

This doesn't make sense.
The IsNullOrEmpty is a static method that is built correctly.
The exception is not caused on the IsNullOrEmpty, but on the test() method.
On the for() line, as a matter of fact, and I can't understand why.

Wednesday, April 05, 2006 12:56 AM by Ayende Rahien

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Well, this might sound stupid.

But, can you assign null to a string explicitly?

Wednesday, April 05, 2006 1:24 AM by Kalpesh

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Kalpesh, yes fo course you can assign a null to a string as it is a reference type.

Wednesday, April 05, 2006 1:34 AM by bill

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Ayende,

I beleive that the removal of the code that checks for null is the likely suspect. It's that kind of optimisation that the JIT looks for when you allow it to optimise the code. Rember the JIT can inline code from other assemblies if it thinks it should.

I have bugged the issue on MSDN feedback:
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=a184aa36-55d5-45d3-a308-9c2240a175fe

Wednesday, April 05, 2006 1:36 AM by bill

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

very interesting. and to think that when you asked "have you spotted the problem?" all i could see was an if statement and a method call within a for loop :)

c# rules!

Wednesday, April 05, 2006 11:13 AM by Darren Kopp

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

I am the tester for the JIT. We are looking at the problem since it is most likely a bug on our side.

Wednesday, April 05, 2006 12:41 PM by Jonathan de Halleux

# re: DANGER ! String.IsNullOrEmpty can lead to runtime Null exceptions !!

Thanks Jonathan. I look forward to finding out why this happens.

Wednesday, April 05, 2006 6:34 PM by bill

# re: Null, Nothing, Empty

Thursday, April 06, 2006 11:55 AM by XTab's Blog

# Be careful with String.IsNullOrEmpty

While I ran FxCop over someone else's code it pointed me to a String method I didn't know about: IsNullOrEmpty

Tuesday, September 19, 2006 4:07 AM by Jean-Paul Smit

# Thomas goes .NET - String.IsNullOrEmpty() schmei??t Exceptions

PingBack from http://blog.thomasbandt.de/PermaLink,guid,d6de00ed-ab10-485a-bda4-615e53eb3981.aspx

# String.IsNullOrEmpty can lead to runtime Null exceptions!

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Sunday, June 17, 2007 9:23 AM by DotNetKicks.com

# String.IsNulOrEmpty Flaw

String.IsNulOrEmpty Flaw

Monday, June 18, 2007 11:12 AM by Dityo Nurasto Journal

# Okay this is nice, trust me | Suddenelfilio 's Blog

Pingback from  Okay this is nice, trust me | Suddenelfilio 's Blog

Wednesday, June 20, 2007 2:45 AM by Okay this is nice, trust me | Suddenelfilio 's Blog

# dangerous behaviour of String.IsNullOrEmpty at www2.sitina.net

Pingback from  dangerous behaviour of String.IsNullOrEmpty at  www2.sitina.net

# Bug en String.IsNullOrEmpty en .NET 2.0

Leo en un blog de MSMVPs.com que se ha detectado un bug en el método estático String.IsNullOrEmpty causado

Sunday, July 29, 2007 6:58 PM by vtortola

# El bug de String.IsNullOrEmpty en .NET 2.0

Leo en un blog de MSMVPs.com una noticia de hace más de un año que contaba que se detecto un bug en el

Monday, July 30, 2007 2:11 PM by vtortola